'WinAppDriver & Python error: AttributeError: 'dict' object has no attribute 'click'

I have a mini project that needs to automate tasks on GUI windows app using Python.

While trying to find a solution I found WinAppDriver which is Selenium-like.

When I try to run the python sample code it's returned this error: "AttributeError: 'dict' object has no attribute 'click'"

Here the python code:

from appium import webdriver

class SimpleCalculatorTests(unittest.TestCase):

    @classmethod

    def setUpClass(self):
        #set up appium
        desired_caps = {}
        desired_caps["app"] = "Microsoft.WindowsCalculator_8wekyb3d8bbwe!App"
        self.driver = webdriver.Remote(
            command_executor='http://127.0.0.1:4723',
            desired_capabilities= desired_caps)

    @classmethod
    def tearDownClass(self):
        self.driver.quit()

    def getresults(self):
        displaytext = self.driver.find_element_by_accessibility_id("CalculatorResults").text
        displaytext = displaytext.strip("Display is " )
        displaytext = displaytext.rstrip(' ')
        displaytext = displaytext.lstrip(' ')
        return displaytext


    def test_initialize(self):
        self.driver.find_element_by_name("Clear").click()
        self.driver.find_element_by_name("Seven").click()
        self.assertEqual(self.getresults(),"7")
        self.driver.find_element_by_name("Clear").click()

    def test_addition(self):
        self.driver.find_element_by_name("One").click()
        self.driver.find_element_by_name("Plus").click()
        self.driver.find_element_by_name("Seven").click()
        self.driver.find_element_by_name("Equals").click()
        self.assertEqual(self.getresults(),"8")

    def test_combination(self):
        self.driver.find_element_by_name("Seven").click()
        self.driver.find_element_by_name("Multiply by").click()
        self.driver.find_element_by_name("Nine").click()
        self.driver.find_element_by_name("Plus").click()
        self.driver.find_element_by_name("One").click()
        self.driver.find_element_by_name("Equals").click()
        self.driver.find_element_by_name("Divide by").click()
        self.driver.find_element_by_name("Eight").click()
        self.driver.find_element_by_name("Equals").click()
        self.assertEqual(self.getresults(),"8")

    def test_division(self):
        self.driver.find_element_by_name("Eight").click()
        self.driver.find_element_by_name("Eight").click()
        self.driver.find_element_by_name("Divide by").click()
        self.driver.find_element_by_name("One").click()
        self.driver.find_element_by_name("One").click()
        self.driver.find_element_by_name("Equals").click()
        self.assertEqual(self.getresults(),"8")

    def test_multiplication(self):
        self.driver.find_element_by_name("Nine").click()
        self.driver.find_element_by_name("Multiply by").click()
        self.driver.find_element_by_name("Nine").click()
        self.driver.find_element_by_name("Equals").click()
        self.assertEqual(self.getresults(),"81") 

    def test_subtraction(self):
        self.driver.find_element_by_name("Nine").click()
        self.driver.find_element_by_name("Minus").click()
        self.driver.find_element_by_name("One").click()
        self.driver.find_element_by_name("Equals").click()
        self.assertEqual(self.getresults(),"8")

if __name__ == '__main__':
    suite = unittest.TestLoader().loadTestsFromTestCase(SimpleCalculatorTests)
    unittest.TextTestRunner(verbosity=2).run(suite)

And here is the python response:

C:\Users\anas9\PycharmProjects\winappdrivertest\venv\Scripts\python.exe C:/Users/anas9/PycharmProjects/winappdrivertest/main.py
test_addition (__main__.SimpleCalculatorTests) ... C:\Users\anas9\PycharmProjects\winappdrivertest\main.py:51: DeprecationWarning: find_element_by_name is deprecated. Please use find_element(by=By.NAME, value=name) instead
  self.driver.find_element_by_name("One").click()
ERROR
test_combination (__main__.SimpleCalculatorTests) ... C:\Users\anas9\PycharmProjects\winappdrivertest\main.py:58: DeprecationWarning: find_element_by_name is deprecated. Please use find_element(by=By.NAME, value=name) instead
  self.driver.find_element_by_name("Seven").click()
ERROR
test_division (__main__.SimpleCalculatorTests) ... C:\Users\anas9\PycharmProjects\winappdrivertest\main.py:70: DeprecationWarning: find_element_by_name is deprecated. Please use find_element(by=By.NAME, value=name) instead
  self.driver.find_element_by_name("Eight").click()
ERROR
test_initialize (__main__.SimpleCalculatorTests) ... C:\Users\anas9\PycharmProjects\winappdrivertest\main.py:45: DeprecationWarning: find_element_by_name is deprecated. Please use find_element(by=By.NAME, value=name) instead
  self.driver.find_element_by_name("Clear").click()
ERROR
test_multiplication (__main__.SimpleCalculatorTests) ... C:\Users\anas9\PycharmProjects\winappdrivertest\main.py:79: DeprecationWarning: find_element_by_name is deprecated. Please use find_element(by=By.NAME, value=name) instead
  self.driver.find_element_by_name("Nine").click()
ERROR
test_subtraction (__main__.SimpleCalculatorTests) ... C:\Users\anas9\PycharmProjects\winappdrivertest\main.py:86: DeprecationWarning: find_element_by_name is deprecated. Please use find_element(by=By.NAME, value=name) instead
  self.driver.find_element_by_name("Nine").click()
ERROR

======================================================================
ERROR: test_addition (__main__.SimpleCalculatorTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\anas9\PycharmProjects\winappdrivertest\main.py", line 51, in test_addition
    self.driver.find_element_by_name("One").click()
AttributeError: 'dict' object has no attribute 'click'

======================================================================
ERROR: test_combination (__main__.SimpleCalculatorTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\anas9\PycharmProjects\winappdrivertest\main.py", line 58, in test_combination
    self.driver.find_element_by_name("Seven").click()
AttributeError: 'dict' object has no attribute 'click'

======================================================================
ERROR: test_division (__main__.SimpleCalculatorTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\anas9\PycharmProjects\winappdrivertest\main.py", line 70, in test_division
    self.driver.find_element_by_name("Eight").click()
AttributeError: 'dict' object has no attribute 'click'

======================================================================
ERROR: test_initialize (__main__.SimpleCalculatorTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\anas9\PycharmProjects\winappdrivertest\main.py", line 45, in test_initialize
    self.driver.find_element_by_name("Clear").click()
AttributeError: 'dict' object has no attribute 'click'

======================================================================
ERROR: test_multiplication (__main__.SimpleCalculatorTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\anas9\PycharmProjects\winappdrivertest\main.py", line 79, in test_multiplication
    self.driver.find_element_by_name("Nine").click()
AttributeError: 'dict' object has no attribute 'click'

======================================================================
ERROR: test_subtraction (__main__.SimpleCalculatorTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\anas9\PycharmProjects\winappdrivertest\main.py", line 86, in test_subtraction
    self.driver.find_element_by_name("Nine").click()
AttributeError: 'dict' object has no attribute 'click'

----------------------------------------------------------------------
Ran 6 tests in 1.314s

FAILED (errors=6)

Process finished with exit code 0

The example available here: WinAppDriver/tree/master/Samples/Python

Thanks,



Solution 1:[1]

I found the solution, which is to revert Selenium to 3.141.0 by running:

pip install selenium==3.141.0

Source

Solution 2:[2]

To elaborate on the answer from Anas H, the driver is returning a dictionary but it should be returning a WebElement. In short, WinAppDriver does not seem to have full Selenium 4 support at the moment, however it does support Selenium 3.14. Use the pip command offered by Anas H

pip install selenium==3.14

or for PyCharm, under Python Packages you can type in Selenium and delete your current selenium package. Research for Selenium and instead of Selenium 4, choose your desired Selenium 3.1 version, again I am using 3.14.

If for some reason you choose to stick with Selenium 4, others issues you may come across in the example you provided would be the "find_element()" function. You would need to import "By" from selenium.webdriver.common.by in order to find any particular element.

# Selenium 3
self.driver.find_element_by_name("Nine").click()

# Selenium 4
from selenium.webdriver.common.by import By
self.driver.find_element(By.NAME, "Nine")

https://github.com/microsoft/WinAppDriver/issues/1638

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 David
Solution 2