appium / python-client

Python language bindings for Appium
Apache License 2.0
1.61k stars 554 forks source link

AttributeError: 'NoneType' object has no attribute 'to_capabilities' #985

Closed patrykgamergod closed 1 week ago

patrykgamergod commented 1 week ago

The problem

PS C:\Users\patry> & C:/Users/patry/AppData/Local/Programs/Python/Python312/python.exe c:/Users/patry/Downloads/greggs.py Traceback (most recent call last): File "c:\Users\patry\Downloads\greggs.py", line 17, in driver = webdriver.Remote(appium_server, desired_caps) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\patry\AppData\Local\Programs\Python\Python312\Lib\site-packages\appium\webdriver\webdriver.py", line 229, in init super().init( File "C:\Users\patry\AppData\Local\Programs\Python\Python312\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 188, in init capabilities = options.to_capabilities() ^^^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'NoneType' object has no attribute 'to_capabilities' PS C:\Users\patry>

im trying to automate a application on android studios and im currently coding on visual studio code python and i keep getting the same error and im getting annoyed since i thought this proccess would be easy as ive done website automating

Environment

Details

i keep getting the same error i dont know what im doing wrong if someone could help me that would be appreciated, im trying to automate an app and it didnt work so i tried a simple app such as calculator and it still didnt work and somehow on command prompt nothing shows up when i got appium on command prompt so im confused

Link to Appium Logs

no logs as im aware of Create a GIST which is a paste of your full Appium logs, and link them here.

Code To reproduce issue

from appium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC

Desired capabilities for the virtual device

desired_caps = { "platformName": "Android", "deviceName": "Pixel 8 Pro API 34", # Replace with your device name "appPackage": "com.android.calculator2", # Calculator package name "appActivity": ".Calculator", # Calculator activity name }

Appium server address

appium_server = 'http://localhost:4723/wd/hub'

Create a WebDriver instance

driver = webdriver.Remote(appium_server, desired_caps)

Find and click on the number 2 button

button_2 = WebDriverWait(driver, 10).until( EC.presence_of_element_located(("id", "digit_2")) ) button_2.click()

Find and click on the plus button

button_plus = WebDriverWait(driver, 10).until( EC.presence_of_element_located(("id", "op_add")) ) button_plus.click()

Find and click on the number 3 button

button_3 = WebDriverWait(driver, 10).until( EC.presence_of_element_located(("id", "digit_3")) ) button_3.click()

Find and click on the equals button

button_equals = WebDriverWait(driver, 10).until( EC.presence_of_element_located(("id", "eq")) ) button_equals.click()

Get the result

result = WebDriverWait(driver, 10).until( EC.presence_of_element_located(("id", "result")) ).text

print("Result:", result)

Quit the driver

driver.quit()

KazuCocoa commented 1 week ago

Duplicate of https://github.com/appium/python-client/issues/918 , which is selenium webdriver's version dependent issue. Please check https://github.com/appium/python-client?tab=readme-ov-file#compatibility-matrix

patrykgamergod commented 1 week ago

hi yes but that isnt the issue my appium is on Version: 4.0.0 while my selenium is on Version: 4.20.0

KazuCocoa commented 1 week ago

Then, you should follow Options way instead of desired_caps

https://github.com/appium/python-client?tab=readme-ov-file#quick-migration-guide-from-v2-to-v3 https://github.com/appium/python-client?tab=readme-ov-file#usage

patrykgamergod commented 1 week ago

i used the usage example and it still didnt work? File "C:\Users\patry\AppData\Local\Programs\Python\Python312\Lib\site-packages\appium\webdriver\webdriver.py", line 229, in init super().init( File "C:\Users\patry\AppData\Local\Programs\Python\Python312\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 188, in init capabilities = options.to_capabilities() ^^^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'NoneType' object has no attribute 'to_capabilities'

i still get the same error? what am i doing wrong?

KazuCocoa commented 1 week ago

Could you share your current code? I'd like to see especially what options key was given in your code as example in https://github.com/appium/python-client?tab=readme-ov-file#usage

webdriver.Remote(f'http://{APPIUM_HOST}:{APPIUM_PORT}', options=options)
patrykgamergod commented 1 week ago

hi im not getting any errors anymore but nothing is happening and theres no tests running and nothing shows on command prompt, i am new to this so guidence would be helpful from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver from appium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC

import pytest

from appium import webdriver

Options are only available since client version 2.3.0

If you use an older client then switch to desired_capabilities

instead: https://github.com/appium/python-client/pull/720

from appium.options.android import UiAutomator2Options from appium.options.ios import XCUITestOptions from appium.webdriver.appium_service import AppiumService from appium.webdriver.common.appiumby import AppiumBy

APPIUM_PORT = 4723 APPIUM_HOST = '127.0.0.1'

HINT: fixtures below could be extracted into conftest.py

HINT: and shared across all tests in the suite

@pytest.fixture(scope='session') def appium_service(): service = AppiumService() service.start(

Check the output of appium server --help for the complete list of

    # server command line arguments
    args=['--address', APPIUM_HOST, '-p', str(APPIUM_PORT)],
    timeout_ms=20000,
)
yield service
service.stop()

def create_android_driver(custom_opts = None): options = UiAutomator2Options() options.platformVersion = '14' options.udid = '123456789ABC' if custom_opts is not None: options.load_capabilities(custom_opts)

Appium1 points to http://127.0.0.1:4723/wd/hub by default

return webdriver.Remote(f'http://localhost:4723/wd/hub', options=options)

Write your test case

def test_open_app(android_driver):

Replace 'com.example.app' with your app's package name

android_driver.start_activity(app_package='com.mobile5.greggs', app_activity='.MainActivity')

@pytest.fixture def android_driver_factory(): return create_android_driver

if possible you could tell me what im doing wrong as im trying to automate on android studios a virtual device on visual studio code python

KazuCocoa commented 1 week ago

What value was passed https://github.com/appium/python-client/blob/master/appium/webdriver/webdriver.py#L229-L231 in your local environment? The error usually occurs when the options pass as None.

then, https://github.com/SeleniumHQ/selenium/blob/trunk/py/selenium/webdriver/remote/webdriver.py#L164-L170 's options is None and https://github.com/SeleniumHQ/selenium/blob/trunk/py/selenium/webdriver/remote/webdriver.py#L188 refers to None.

patrykgamergod commented 1 week ago

i dont know where to check im new to appium as its more complicated than selenium, could i have some template where i could change the values and have the code work? as i cant seem to figure this out and it would be useful

KazuCocoa commented 1 week ago

It is your local Python code debugging, not appium itself.

patrykgamergod commented 1 week ago

Well the code I used didn't show anything in the terminal and nothing in appium command prompt so I'm lost, I'm not sure what to do next and don't get how appium works and a template would be easier since I will understand what to do. I get selenium since I wrote code to automate websites but don't understand appium at all

On Sun, 5 May 2024, 18:29 Kazuaki Matsuo, @.***> wrote:

It is your local Python code debugging, not appium itself.

— Reply to this email directly, view it on GitHub https://github.com/appium/python-client/issues/985#issuecomment-2094886766, or unsubscribe https://github.com/notifications/unsubscribe-auth/AOK47I367TMELYCAZCRZ7WDZAZT7TAVCNFSM6AAAAABHG7XTFWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDAOJUHA4DMNZWGY . You are receiving this because you authored the thread.Message ID: @.***>

KazuCocoa commented 1 week ago

Perhaps adding print in your code directly would be the easiest method. You code might exist in: C:\Users\patry\AppData\Local\Programs\Python\Python312\Lib\site-packages\appium\webdriver\webdriver.py C:\Users\patry\AppData\Local\Programs\Python\Python312\Lib\site-packages\selenium\webdriver\remote\webdriver.py based on your past log. Then, please make sure the code's appium python lib version and selenium versions follow https://github.com/appium/python-client?tab=readme-ov-file#compatibility-matrix

If you use selenium 4.20.0, please make sure your appium python lib is 3.0.0+.