SeleniumHQ / selenium

A browser automation framework and ecosystem.
https://selenium.dev
Apache License 2.0
30.5k stars 8.15k forks source link

[🐛 Bug]: Python + Selenium 4.0+ (AttributeError: 'dict' object has no attribute 'click') #10171

Closed ElenaStepuro closed 2 years ago

ElenaStepuro commented 2 years ago

What happened?

During running tests on Python faced with issue: AttributeError: 'dict' object has no attribute 'click' which didn't see on Selenium 3.141

How can we reproduce the issue?

Python 3.8
Exception see with w3c=True and without

Code:
    def setup_method(self):
        options = webdriver.ChromeOptions()
        options.w3c = True
        options.add_argument('--no-sandbox')
        self.driver = webdriver.Remote(
            command_executor="http://localhost:8085",
            desired_capabilities=webdriver.DesiredCapabilities.CHROME,
            options=options)

Test code:
    def test(self, setup_method):
        self.driver.get('https://sha-test-app.herokuapp.com/')
        self.driver.find_element(By.XPATH, "//button[contains(@class,'default-btn')]").click()

Relevant log output

def test(self, setup_method):
        self.driver.get('https://sha-test-app.herokuapp.com/')
        self.button=self.driver.find_element(By.XPATH, "//button[contains(@class,'default-btn')]")
>       self.button.click()
E       AttributeError: 'dict' object has no attribute 'click'

Operating System

Windows 10

Selenium version

4.0.0 and 4.1.0

What are the browser(s) and version(s) where you see this issue?

Chrome 94, 95, 96

What are the browser driver(s) and version(s) where you see this issue?

Remote

Are you using Selenium Grid?

No

github-actions[bot] commented 2 years ago

@ElenaStepuro, thank you for creating this issue. We will troubleshoot it as soon as we can.


Info for maintainers

Triage this issue by using labels.

If information is missing, add a helpful comment and then I-issue-template label.

If the issue is a question, add the I-question label.

If the issue is valid but there is no time to troubleshoot it, consider adding the help wanted label.

If the issue requires changes or fixes from an external project (e.g., ChromeDriver, GeckoDriver, W3C), add the applicable G-* label, and it will provide the correct link and auto-close the issue.

After troubleshooting the issue, please add the R-awaiting answer label.

Thank you!

titusfortner commented 2 years ago

First, this error doesn't make sense. If Selenium is getting a return value from find_element() it isn't going to be a dict unless something is getting monkey patched somewhere.

I still tried to reproduce with a Selenium server, but this code works fine for me:

    options = ChromeOptions()
    options.w3c = True
    options.add_argument('--no-sandbox')
    driver = Remote(
        command_executor="http://localhost:4444",
        desired_capabilities=DesiredCapabilities.CHROME,
        options=options)
    driver.get('https://sha-test-app.herokuapp.com/')
    element = driver.find_element(By.XPATH, "//button[contains(@class,'default-btn')]")
    element.click()

That said, you shouldn't be setting w3c any longer, or using desired_capabilities, and you should avoid bypassing the security sandbox unless you've got a good use case for it.

What is running on port 8085? If a Selenium server, what version? If it's a chromedriver, why not use webdriver.Chrome() for this instead?

ElenaStepuro commented 2 years ago

Hello @titusfortner ! I've retried to run code without setting w3c and still has the same exception. On port 8085 I'm running docker container which I have to called during my test run. Forget to add while opening this issue: on port 8086 I'm running container which used Selenium Grid 4.0.0-rc-1

dongfangtianyu commented 2 years ago

I encountered a similar problem during the use of Appium, image

At this time, find_elenment returned a dict instead of a WebElement.

Unfortunately I cannot reproduce this problem,

Can you print out the return value of find_element, @ElenaStepuro ? Or maybe provide an environment for others to try ?I'm afraid it's not accidental,

titusfortner commented 2 years ago

Oh, this makes sense, this is the JSON Wire Protocol signature for elements. You aren't getting a w3c response from the driver.

ElenaStepuro commented 2 years ago

Hello @dongfangtianyu ! This is what findElement returns image

titusfortner commented 2 years ago

Somehow you are getting a jwp session. It should default to w3c, so you're doing something weird.

Appium is slightly harder to get a w3c session.

What is standard out on your console?

titusfortner commented 2 years ago

I can duplicate this error by setting:

    options.add_experimental_option('w3c', False)

@ElenaStepuro send the exact code you are using, and the console output from the grid. Something it is seeing wants to toggle JSON Wire Protocol, which should not be happening with what you've provided, so something is missing

@dongfangtianyu Getting w3c compliant Appium session is a little trickier. You can see an example of valid w3c here: https://saucelabs.com/platform/platform-configurator#/

titusfortner commented 2 years ago

Since this is user setting issues and not a problem with Selenium code, I'm closing this. If people need additional assistance, you can @ me here, contact us in Chat

JaapHaitsma commented 2 years ago

I had the same error. For me the solution was to update the chromedriver to latest version

Georgepop commented 2 years ago

yeap, me too

github-actions[bot] commented 2 years ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.