Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing 'Keys' from 'selenium.webdriver.common.keys'

I am trying to execute the below code. I exclusively tried to import Keys from webdriver, but it still does not work.

from selenium import webdriver
import selenium.webdriver.common.keys
driver = webdriver.Firefox()
page = driver.get("https://www.python.org/")
print (driver.title)
finder = driver.find_element_by_class_name("search-field")
finder.send_keys("Python Test")
finder.send_keys(Keys.RETURN)

Output:

Welcome to Python.org
Traceback (most recent call last):
  File "C:/Users/Arvind/Desktop/Python Tests/selenium_tests.py", line 9, in
<module>
    finder.send_keys(Keys.RETURN)
NameError: name 'Keys' is not defined
>>>
like image 980
Arvind T Avatar asked May 07 '17 18:05

Arvind T


People also ask

What is from Selenium Webdriver common Keys?

It's used to ID the different Keys on the keyboard. Ex: Keys. RETURN means your ENTER key in your keyboard. These keys can then be used by selenium for keyboard emulation or other stuff.

How do you enter a key in Selenium?

We can type Enter/Return key in Selenium. We shall use the sendKeys method and pass Keys. ENTER as an argument to the method. Also, we can use pass Keys.

What is the Selenium Webdriver command to use following combination of Keys on the keyboard Ctrl A?

We can perform key press of (CTRL+A) with Selenium Webdriver. There are multiple ways to do this. We can use the Keys. chord() method to simulate this keyboard action.


1 Answers

You need to have

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

import selenium.webdriver.common.keys.

Then your code would run fine.

like image 173
demouser123 Avatar answered Sep 28 '22 05:09

demouser123