6

I am trying to execute a simple test:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()

driver.get('http://google.com')
driver.find_element_by_name('q')
driver.send_keys('hey')

And I am getting the error:

Traceback (most recent call last):
  File "C:/webdriver/test.py", line 8, in <module>
    driver.send_keys('hey')
AttributeError: 'WebDriver' object has no attribute 'send_keys'

What's the problem?

4
  • 1
    Did you try this? driver.find_element_by_name('q').send_keys('hey') Commented Sep 29, 2015 at 3:14
  • 1
    Helping Hands is right: use the send_keys method on the identified WebElement, not on the driver. Commented Sep 29, 2015 at 3:20
  • Thanks! driver.find_element_by_name('q').send_keys('hey') worked for me. But anyway why my code didn't? Commented Sep 29, 2015 at 3:44
  • @PavelPrakopchyk - Cheers...:) your code did not work because as per your code web driver getting confuse that where to send keys? because you have not use find element attribute with send keys so. Commented Sep 29, 2015 at 4:18

6 Answers 6

4

The WebDriver instance does not have a send_keys() method. That's what the error is actually about:

'WebDriver' object has no attribute 'send_keys'

Call send_keys() on a WebElement instance which is returned by find_element_by_*() methods - find_element_by_name() in your case:

element = driver.find_element_by_name('q')
element.send_keys("hey")

And just FYI, there is also an ActionChains class which is useful do build up chains of actions or apply more complex actions like drag&drop or mouse move. It's an overhead in this case, but just for the sake of an example:

from selenium.webdriver.common.action_chains import ActionChains

actions = ActionChains(driver)
actions.move_to_element(element).send_keys("hey").perform()
Sign up to request clarification or add additional context in comments.

2 Comments

driver.find_element_by_name was deprecated and then removed (e.g., isn't in Selenium 4.12)
See e.g. Selenium - Python - AttributeError: 'WebDriver' object has no attribute 'find_element_by_name': "Selenium just removed that method in version 4.3.0... Deprecated find_element_by_* and find_elements_by_* are now removed (#10712)"
1

Did you try changing your reference element?

It would be a good practice if you call webdriver using a different reference.

Your code after changing the reference.

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

driver = webdriver.Firefox()
driver.get('http://google.com')
#Firefox Webdriver is created and navigated to google.

elem = driver.find_element_by_name('q')
elem.send_keys('hey',Keys.RETURN)
#Keys.RETURN = Pressing Enter key on keyboard

time.sleep(5)
driver.close()

1 Comment

find_element_by_name was removed in Selenium 4.3.0.
1

The problem with your code is you do not have to tell the driver where to send the keys:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()

driver.get('http://google.com')
driver.find_element_by_name('q')
driver.send_keys('hey') // The driver doesn’t know where to send the keys

The correct code would be:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()

driver.get('http://google.com')
goog = driver.find_element_by_name('q') // You have stored an element in the 'goog' variable
goog.send_keys('hey') // You told the variable to send 'hey'

Or you can directly send after finding the element like:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()

driver.get('http://google.com')
driver.find_element_by_name('q').send_keys('hey')

1 Comment

find_element_by_name was removed in Selenium 4.3.0.
0

You must change the reference element

driver.get('http://google.com')
elem.find_element_by_name('q')
elem.send_keys('hey')

1 Comment

find_element_by_name was removed in Selenium 4.3.0.
0

I had faced the same issue, but I got a solution for that.

Use move_to_element in ActionChains.

  1. Find the element. elem = driver.find_element_by_*
  2. Create your actionchain driver actions = ActionChains(driver)
  3. use move to command, so webdriver will point to that element's position, but before you send the key, you need to set the webdriver position on that element by using the click() function. Now, webdriver has a surface area to place given keys (data) and for that you will use send_keys() function. In the last just put perform() function to execute these all task very smoothly.

actions.move_to_element(<ELEMENT>).click().send_keys(<DATA>).perform()

https://selenium-python.readthedocs.io/api.htmlt

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()

driver.get('http://google.com')
elem = driver.find_element_by_name('q')
actions = ActionChains(driver)
actions.move_to_element(elem).click().send_keys('hey').perform()

1 Comment

find_element_by_name was removed in Selenium 4.3.0.
-1

It’s with the version of Selenium which is causing the issue. I faced the same issue.

It’s with the Selenium version 3.3.3 which has the compatibility problem.

Try:

pip uninstall selenium
pip install selenium==3.3.1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.