pythonselenium-webdriverappium

AttributeError: 'NoneType' object has no attribute 'to_capabilities'. Getting this error when running a python Script on Appium


import unittest
from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy

capabilities = dict(
    platformName='Android',
    automationName='uiautomator2',
    deviceName='Samsung S9',
    appPackage='com.android.settings',
    appActivity='.Settings',
    language='en',
    locale='US'
)

appium_server_url = 'http://localhost:4723'

class TestAppium(unittest.TestCase):
    def setUp(self) -> None:
        self.driver = webdriver.Remote(appium_server_url, capabilities)

    def tearDown(self) -> None:
        if self.driver:
            self.driver.quit()

    def test_find_battery(self) -> None:
        el = self.driver.find_element(by=AppiumBy.XPATH, value='//*[@text="Battery"]')
        el.click()

if __name__ == '__main__':
    unittest.main()

The above is the example code from official Appium website (http://appium.io/docs/en/2.1/quickstart/test-py/), I have installed all the prequisites requred but still I'm getting the below error when I run the python file:

C:\Users\syeda\Desktop>python test.py
E
======================================================================
ERROR: test_find_battery (__main__.TestAppium.test_find_battery)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\syeda\Desktop\test.py", line 19, in setUp
    self.driver = webdriver.Remote(appium_server_url, capabilities)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\syeda\AppData\Local\Programs\Python\Python311\Lib\site-packages\appium\webdriver\webdriver.py", line 229, in __init__
    super().__init__(
  File "C:\Users\syeda\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 185, in __init__
    capabilities = options.to_capabilities()
                   ^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'to_capabilities'

----------------------------------------------------------------------
Ran 1 test in 0.001s

FAILED (errors=1)

I made sure the Appium server is running.

I'm not sure why this eroor is occuring. I tried searching on the web but no luck.


Solution

  • import unittest
    from appium import webdriver
    from appium.webdriver.common.appiumby import AppiumBy
    
    # Import Appium UiAutomator2 driver for Android platforms (AppiumOptions)
    from appium.options.android import UiAutomator2Options
    
    capabilities = dict(
        platformName='Android',
        automationName='uiautomator2',
        deviceName='Samsung S9',
        appPackage='com.android.settings',
        appActivity='.Settings',
        language='en',
        locale='US'
    )
    
    appium_server_url = 'http://localhost:4723'
    
    # Converts capabilities to AppiumOptions instance
    capabilities_options = UiAutomator2Options().load_capabilities(capabilities)
    
    class TestAppium(unittest.TestCase):
        def setUp(self) -> None:
            self.driver = webdriver.Remote(command_executor=appium_server_url,options=capabilities_options)
    
        def tearDown(self) -> None:
            if self.driver:
                self.driver.quit()
    
        def test_find_battery(self) -> None:
            el = self.driver.find_element(by=AppiumBy.XPATH, value='//*[@text="Battery"]')
            el.click()
    
    if __name__ == '__main__':
        unittest.main()