Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

find_element() returns dict instead of WebElement. #654

Closed
knttnk opened this issue Nov 18, 2021 · 15 comments
Closed

find_element() returns dict instead of WebElement. #654

knttnk opened this issue Nov 18, 2021 · 15 comments

Comments

@knttnk
Copy link

knttnk commented Nov 18, 2021

The problem

When I call find_element, it returns dict instead of WebElement.

Environment

  • Appium version (or git revision) that exhibits the issue: 2.0.0
  • Last Appium version that did not exhibit the issue (if applicable): 1.3.0
  • Desktop OS/version used to run Appium: Windows 10
  • Node.js version (unless using Appium.app|exe): -
  • Npm or Yarn package manager: -
  • Mobile platform/version under test: Windows 10
  • Real device or emulator/simulator: Real device
  • Appium CLI or Appium.app|exe: CLI

Code To Reproduce Issue

# in Command Prompt
# "C:\Program Files (x86)\Windows Application Driver\WinAppDriver.exe" 4723

from appium import webdriver
from selenium.webdriver.common.by import By

alarm_clock_session = webdriver.Remote(
    command_executor='http://127.0.0.1:4723',
    desired_capabilities={
        "app": "Microsoft.WindowsAlarms_8wekyb3d8bbwe!App",
    },
)

settings = alarm_clock_session.find_element(
    By.NAME,
    "設定",  # means "settings" in Japanese
)
print(type(settings))  # <class 'dict'>, not <class 'WebElement'>
settings.click()  # AttributeError: 'dict' object has no attribute 'click'
@KazuCocoa
Copy link
Member

Could you share the appium log (esp the response) and Python client dependencies (selenium client version)?
Our CI had https://github.com/appium/python-client/blob/master/test/functional/ios/webdriver_tests.py#L109 . Its internal was

def find_element_by_accessibility_id(self: T, accessibility_id: str) -> 'WebElement':
. It passed then.

@KazuCocoa
Copy link
Member

KazuCocoa commented Nov 18, 2021

Btw, I noticed the app/driver was WinAppDriver.

I think WinAppDriver has not supported W3C webdriver spec yet.
v2 follows selenium client v4, which expects W3C WebDriver spec, so appium drivers that do not support W3C might behave wrong. Then, please keep v1 client.

@knttnk
Copy link
Author

knttnk commented Nov 18, 2021

Could you tell me how to get appium log?

I'm using appium/python 2.0.0 (selenium/4.0.0 (python windows)).

@jlipps
Copy link
Member

jlipps commented Nov 18, 2021

The appium log is the text produced by the appium server. You don't need a client to get it.

@KazuCocoa
Copy link
Member

or you can specify --log https://appium.io/docs/en/writing-running-appium/server-args/ as server arg.

I guess the response does not have element-6066-11e4-a52e-4f735466cecf key as microsoft/WinAppDriver#1610 (comment)

@knttnk
Copy link
Author

knttnk commented Nov 19, 2021

@KazuCocoa
Copy link
Member

It looks like the attached log issued click action endpoint and it got a 200 ok response.
Did AttributeError: 'dict' object has no attribute 'click' occur in this case as well?

[info] �[35m[HTTP]�[39m �[37m-->�[39m �[37mPOST�[39m �[37m/wd/hub/session/7eb97949-d232-4987-99ea-65cfb30b9a3c/element�[39m
[info] �[35m[HTTP]�[39m �[90m{"using":"name","value":"設定"}�[39m
[debug] �[35m[W3C (7eb97949)]�[39m Calling AppiumDriver.findElement() with args: ["name","設定","7eb97949-d232-4987-99ea-65cfb30b9a3c"]
[debug] �[35m[BaseDriver]�[39m Valid locator strategies for this request: xpath, id, name, class name, accessibility id
[debug] �[35m[WD Proxy]�[39m Matched '/element' to command name 'findElement'
[debug] �[35m[WD Proxy]�[39m Proxying [POST /element] to [POST http://127.0.0.1:4724/wd/hub/session/48EEE802-1B1E-4917-82A2-B66E0C527F67/element] with body: {"using":"name","value":"設定"}
[debug] �[35m[WinAppDriver]�[39m ==========================================
[debug] �[35m[WinAppDriver]�[39m POST /wd/hub/session/48EEE802-1B1E-4917-82A2-B66E0C527F67/element HTTP/1.1

[debug] �[35m[WinAppDriver]�[39m Accept: application/json, */*

[debug] �[35m[WinAppDriver]�[39m Connection: keep-alive

[debug] �[35m[WinAppDriver]�[39m Content-Length: 33

[debug] �[35m[WinAppDriver]�[39m Content-Type: application/json; charset=utf-8

[debug] �[35m[WinAppDriver]�[39m Host: 127.0.0.1:4724

[debug] �[35m[WinAppDriver]�[39m User-Agent: appium
[debug] �[35m[WinAppDriver]�[39m HTTP/1.1 200 OK

[debug] �[35m[WinAppDriver]�[39m Content-Length: 101

[debug] �[35m[WinAppDriver]�[39m Content-Type: application/json

[debug] �[35m[WinAppDriver]�[39m 

[debug] �[35m[WinAppDriver]�[39m {"sessionId":"48EEE802-1B1E-4917-82A2-B66E0C527F67","status":0,"value":{"ELEMENT":"42.1377972.4.55"}}
[debug] �[35m[WD Proxy]�[39m Got response with status 200: {"sessionId":"48EEE802-1B1E-4917-82A2-B66E0C527F67","status":0,"value":{"ELEMENT":"42.1377972.4.55"}}
[debug] �[35m[W3C (7eb97949)]�[39m Responding to client with driver.findElement() result: {"element-6066-11e4-a52e-4f735466cecf":"42.1377972.4.55","ELEMENT":"42.1377972.4.55"}
[info] �[35m[HTTP]�[39m �[37m<-- POST /wd/hub/session/7eb97949-d232-4987-99ea-65cfb30b9a3c/element �[39m�[32m200�[39m �[90m93 ms - 95�[39m
[info] �[35m[HTTP]�[39m �[90m�[39m
[info] �[35m[HTTP]�[39m �[37m-->�[39m �[37mPOST�[39m �[37m/wd/hub/session/7eb97949-d232-4987-99ea-65cfb30b9a3c/element/42.1377972.4.55/click�[39m
[info] �[35m[HTTP]�[39m �[90m{"id":"42.1377972.4.55"}�[39m
[info] �[35m[W3C (7eb97949)]�[39m Driver proxy active, passing request on via HTTP proxy
[debug] �[35m[WD Proxy]�[39m Matched '/wd/hub/session/7eb97949-d232-4987-99ea-65cfb30b9a3c/element/42.1377972.4.55/click' to command name 'click'
[debug] �[35m[WD Proxy]�[39m Proxying [POST /wd/hub/session/7eb97949-d232-4987-99ea-65cfb30b9a3c/element/42.1377972.4.55/click] to [POST http://127.0.0.1:4724/wd/hub/session/48EEE802-1B1E-4917-82A2-B66E0C527F67/element/42.1377972.4.55/click] with body: {"id":"42.1377972.4.55"}
[debug] �[35m[WinAppDriver]�[39m ==========================================
[debug] �[35m[WinAppDriver]�[39m POST /wd/hub/session/48EEE802-1B1E-4917-82A2-B66E0C527F67/element/42.1377972.4.55/click HTTP/1.1

[debug] �[35m[WinAppDriver]�[39m Accept: application/json, */*

[debug] �[35m[WinAppDriver]�[39m Connection: keep-alive

[debug] �[35m[WinAppDriver]�[39m Content-Length: 24

[debug] �[35m[WinAppDriver]�[39m Content-Type: application/json; charset=utf-8

[debug] �[35m[WinAppDriver]�[39m Host: 127.0.0.1:4724

[debug] �[35m[WinAppDriver]�[39m User-Agent: appium

[debug] �[35m[WinAppDriver]�[39m 

[debug] �[35m[WinAppDriver]�[39m {"id":"42.1377972.4.55"}[debug] �[35m[WD Proxy]�[39m Got response with status 200: {"sessionId":"48EEE802-1B1E-4917-82A2-B66E0C527F67","status":0}
[info] �[35m[WD Proxy]�[39m Replacing sessionId 48EEE802-1B1E-4917-82A2-B66E0C527F67 with 7eb97949-d232-4987-99ea-65cfb30b9a3c
[info] �[35m[HTTP]�[39m �[37m<-- POST /wd/hub/session/7eb97949-d232-4987-99ea-65cfb30b9a3c/element/42.1377972.4.55/click �[39m�[32m200�[39m �[90m314 ms - 65�[39m
[info] �[35m[HTTP]�[39m �[90m�[39m
[debug] �[35m[WinAppDriver]�[39m HTTP/1.1 200 OK

[debug] �[35m[WinAppDriver]�[39m Content-Length: 63

[debug] �[35m[WinAppDriver]�[39m Content-Type: application/json

[debug] �[35m[WinAppDriver]�[39m 

[debug] �[35m[WinAppDriver]�[39m {"sessionId":"48EEE802-1B1E-4917-82A2-B66E0C527F67","status":0}

@KazuCocoa
Copy link
Member

The log had the below. The WinAppDriver still did not support W3C spec.

[debug] �[35m[WinAppDriver]�[39m {"sessionId":"48EEE802-1B1E-4917-82A2-B66E0C527F67","status":0,"value":{"ELEMENT":"42.1377972.4.55"}}
[debug] �[35m[WD Proxy]�[39m Got response with status 200: {"sessionId":"48EEE802-1B1E-4917-82A2-B66E0C527F67","status":0,"value":{"ELEMENT":"42.1377972.4.55"}}

Some commands should not work with Appium Python client v2 as my previous comment in #654 (comment)

@mykola-mokhnach
Copy link
Contributor

I see Appium does patch responses, so the client receives the correct response:

[debug] �[35m[WinAppDriver]�[39m {"sessionId":"48EEE802-1B1E-4917-82A2-B66E0C527F67","status":0,"value":{"ELEMENT":"42.1377972.4.55"}}
[debug] �[35m[WD Proxy]�[39m Got response with status 200: {"sessionId":"48EEE802-1B1E-4917-82A2-B66E0C527F67","status":0,"value":{"ELEMENT":"42.1377972.4.55"}}
[debug] �[35m[W3C (7eb97949)]�[39m Responding to client with driver.findElement() result: {"element-6066-11e4-a52e-4f735466cecf":"42.1377972.4.55","ELEMENT":"42.1377972.4.55"}

note the presence of "element-6066-11e4-a52e-4f735466cecf":"42.1377972.4.55" in the last line

@mykola-mokhnach
Copy link
Contributor

So, what python does looks like

        response = self.command_executor.execute(driver_command, params)
        if response:
            self.error_handler.check_response(response)
            response['value'] = self._unwrap_value(
                response.get('value', None))
            return response

and what we get in the client response is not put into value. Perhaps, this is a bug inside of base-driver's WD proxy algo, which patches responses

@KazuCocoa
Copy link
Member

Yes, the patch for the element response worked.

The original reported issue was

settings = alarm_clock_session.find_element(
    By.NAME,
    "設定",  # means "settings" in Japanese
)
print(type(settings))  # <class 'dict'>, not <class 'WebElement'>
settings.click()  # AttributeError: 'dict' object has no attribute 'click'

, but the log had [info] �[35m[HTTP]�[39m �[37m-->�[39m �[37mPOST�[39m �[37m/wd/hub/session/7eb97949-d232-4987-99ea-65cfb30b9a3c/element/42.1377972.4.55/click�[39m.

It means python client sent click endpoint probably without the AttributeError.
To get the error, the settings should be dict. So initially I guessed the response by appium/WAD had only ELEMENT key.

@mykola-mokhnach
Copy link
Contributor

So the code looks good so far. Appium does patch JWP responses properly. Also click action in the log did succeed. I'm not quite sure how it could be successful if the client cannot execute settings.click()

@knttnk
Copy link
Author

knttnk commented Nov 19, 2021

Sorry for the complication.

I wasn't using Appium when I had the first problem, but when I logged it I used Appium Server v1.22.0 with GUI.

When I logged it, the Attribute Error did not occur as you said.

@KazuCocoa
Copy link
Member

Appium version (or git revision) that exhibits the issue: 2.0.0
Last Appium version that did not exhibit the issue (if applicable): 1.3.0

So, this meant appium-python-client version, and the issue occurred when you used appium-python-client to WinAppDriver without appium.

Is it correct?

Then, there is no proxy layer to convert protocol between the client and the WAD as appium does.
You should keep using v1 appium python cleint for it until WAD upgrades their protocol to W3C one.

@knttnk
Copy link
Author

knttnk commented Nov 20, 2021

I think it is correct.
Thank you.

@knttnk knttnk closed this as completed Nov 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants