简体   繁体   中英

Python selenium - AttributeError: 'dict' object has no attribute 'get_attribute'

I wanted to showcase selenium and instead of starting a complete Java / C# suite I wanted to quickly use Python since it can be done with a single script.

from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By

print("sample test case started")
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("MyUrl")
element = driver.find_element(By.ID, "Username")
element.send_keys("MyUsername")

driver.close()
print("sample test case successfully completed")

I am getting this error message: AttributeError: 'dict' object has no attribute 'send_keys'

I would seam that find_element method is returning some kind of dict (Dictonary??) object instead of a selenium web element. How can I fix this??

Selenium project is moving to only support W3C and Chrome (since version 75) is using W3C by default. You can run into an issue that you described, if you have old version of chromedriver and selenium >=4, which was the problem in my case. Update your chromedriver or downgrade Selenium version to <4 and it should fix your problem.

I reproduced this problem using Selenium 4.1.0 and Chromedriver 98, by disabling w3c:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_experimental_option("w3c", False)
driver = webdriver.Chrome(options=options)

So, it's another option that could lead to this error. You can fix it by removing it or changing its value to True.

I ran into this exact issue. It seems to be some sort of bug in Selenium 4.0. I have reverted to 3.141.0 ( pip install selenium==3.141.0 ) and it seems to have resolved this issue.

Dear friend, there is a problem with the browser Learn about your version browser from the section Then refer to the following site To your browser version Download the desired drive

https://chromedriver.chromium.org/downloads

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM