Resolving TypeError in Selenium 4.10 When Using Appium with Desired Capabilities

Resolving TypeError in Selenium 4.10 When Using Appium with Desired Capabilities

5 April 2024 Stephan Petzl Leave a comment Tech-Help

If you’re encountering a TypeError when attempting to run your Appium scripts with Selenium 4.10, you’re not alone. This issue typically presents itself with the following error message:

TypeError: WebDriver.__init__() got an unexpected keyword argument ‘desired_capabilities’

This error can occur when attempting to initialize the WebDriver with the desired_capabilities argument, which is no longer supported in the same way as of Selenium 4.10.0.

Understanding the Issue

The root cause of this problem lies in changes made to the WebDriver’s initialization method in Selenium 4.10.0. The desired_capabilities argument has been removed, necessitating a new approach to passing capabilities to the WebDriver.

Implementing the Solution

To resolve this issue, you should follow the updated documentation on how to pass desired capabilities with Selenium 4.10.0 and newer versions. The approach involves using the WebDriver options to set capabilities. Here is an example demonstrating how to adapt your existing code:

from selenium.webdriver.firefox.options import Options as FirefoxOptions
options = FirefoxOptions()
cloud_options = {}
cloud_options['build'] = "build_1"
cloud_options['name'] = "test_abc"
options.set_capability('cloud:options', cloud_options)
driver = webdriver.Remote("http://0.0.0.0:4723/wd/hub", options=options)
            

This code snippet shows how to create an instance of the Options class for your browser (in this case, Firefox), set your desired capabilities within it, and then pass the options object when initializing the webdriver.Remote instance.

Adapting the Solution for Appium

When using Appium, the process is similar. You’ll need to translate your desired capabilities into the new format and use the appropriate options class for your context. Here’s how you can adapt the capabilities for Appium:

from appium.webdriver.common.mobileby import MobileBy
from selenium.webdriver import DesiredCapabilities
from appium.webdriver.appium_service import AppiumService
from appium.webdriver.webdriver import WebDriver

appium_service = AppiumService()
appium_service.start()

desired_caps = {
    "platformName": "Android",
    "appium:platformVersion": "11.0",
    "appium:deviceName": "emulator-5554",
    "appium:app": "/path/to/your/app.apk",
    "appium:automationName": "UiAutomator2",
    "appium:appPackage": "com.example.package",
    "appium:appActivity": "com.example.package.MainActivit"
}

driver = WebDriver(command_executor='http://0.0.0.0:4723/wd/hub', desired_capabilities=desired_caps)
driver.implicitly_wait(20)
            

Make sure to replace the placeholder values with your actual app paths and package names. This updated snippet will help you to bypass the TypeError and successfully run your Appium tests with Selenium 4.10.

Conclusion

Adjusting your code to align with the latest updates in Selenium is crucial for maintaining the functionality of your automated testing scripts. By using the new method of setting capabilities, you can ensure compatibility with Selenium 4.10 and beyond, allowing you to continue testing your applications effectively.

Like this article? there’s more where that came from!