4

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??

9
  • 1
    There is only 1 element with that id. element[0] -> I then get an error: KeyError: 0. Commented Oct 15, 2021 at 9:47
  • Same error. So seams an error in selenium? Commented Oct 15, 2021 at 9:53
  • In my origin question you can see the original code. I now replaced driver.find.element(By.ID, "Username") with element = driver.find_element_by_id("Username") Commented Oct 15, 2021 at 9:58
  • Seems like its is a very weird behaviour of selenium. Please let us delete all the unnecessary comments. Commented Oct 15, 2021 at 10:00
  • My mistake I whooped over dict again. Can you please give the output of (print(element)) Commented Oct 15, 2021 at 10:20

2 Answers 2

7

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.

6

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.

1
  • Move this answer to the top! It should've been accepted!
    – matebende
    Commented Mar 17, 2022 at 14:22

Not the answer you're looking for? Browse other questions tagged or ask your own question.