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

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

You need to have
from selenium.webdriver.common.keys import Keys instead of
import selenium.webdriver.common.keys.
Then your code would run fine.

Related

Simple Selenium Webdriver importing doesnt work

**from selenium import webdriver
browser = webdriver.chrome("./chromedriver.exe")
browser.get("https://www.google.com")
I wrote the above code in visual studio code; however, every time I do this, this error occurs:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'browser' is not defined
I downloaded the Selenium library and web driver matches my Chrome, but I'm unsure what to do.
I Googled and YouTubed it for almost 3 hours and it does not work at all.
Please help me solve this problem.
Start learning Selenium from any of the online tutorials.
Always careful on case sensitive while coding through Selenium with Python:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
browser = webdriver.Chrome(service=Service("<path of the chromedriver.exe>"))
browser.get("https://www.google.com")

why I get __init__.py problems when using selenium webdrivers chrome?

im working on a project and have this as code:
import selenium
from selenium import webdriver
driver = webdriver.Chrome()
print(driver.current_url)
but i get an error and i dont know how to fix it, well mutle tracebacks to error i think, here is the error:
Traceback (most recent call last):
File "C:\Users\ytty\PycharmProjects\test hack\main.py", line 2, in <module>
from selenium import webdriver
File "C:\Users\ytty\PycharmProjects\test hack\venv\lib\site-packages\selenium\webdriver\__init__.py", line 18, in <module>
from .firefox.webdriver import WebDriver as Firefox # noqa
File "C:\Users\ytty\PycharmProjects\test hack\venv\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 36, in <module>
from .service import Service
File "C:\Users\ytty\PycharmProjects\test hack\venv\lib\site-packages\selenium\webdriver\firefox\service.py", line 21, in <module>
class Service(service.Service):
AttributeError: module 'selenium.webdriver.common.service' has no attribute 'Service'
Process finished with exit code 1
can anyone help?
I do it this way:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches", ["enable-logging"])
driver = webdriver.Chrome(options=options, executable_path=ChromeDriverManager().install())
This way it will automatically download the optimal ChromeDriver version on your cache and use it every time you need. Also the experimental part helps to avoid some error that might appear due to driver bugs

Why am I getting NameError: name 'ActionChains' is not defined?

Im pretty new to python and trying to fill out a web form automated.
Im getting this Error:
Traceback (most recent call last):
File "main.py", line 24, in
ActionChains(browser)\
NameError: name 'ActionChains' is not defined
And this is my code:
from time import sleep
from selenium import webdriver
browser = webdriver.Chrome ('/Users/max/Downloads/chromedriver')
browser.get ('http://www.brix.de/computer/web_html_php_et_al/formular-test_smm_01.html')
inputs = browser.find_element_by_xpath(
'/html/body/form[1]/table')
ActionChains(browser)\
.move_to_element(input[vorname]).click()\
.send_keys('name')\
.move_to_element(input[name]).click()\
.send_keys('Surname')\
.perform()
Can somebody help me please?
I think you are missing the import, try the following:
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
browser = webdriver.Chrome ('/Users/kiran/Downloads/chromedriver')
browser.get ('http://www.brix.de/computer/web_html_php_et_al/formular-test_smm_01.html')
inputs = browser.find_element_by_xpath(
'/html/body/form[1]/table')
ActionChains(browser)\
.move_to_element(input[name]).click()\
.send_keys('name')\
.move_to_element(input[vorname]).click()\
.send_keys('Surname')\
.perform()

driver.get on selenium giving error

I have tried running the following command after I've imported everything I need (selenium, webdriver, keys):
>>> driver.get('https://steemit.com/')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'selenium.webdriver.chrome' has no attribute 'get'
I can't find any solution.
You are trying to call .get() method on a module. Instead, you need to instantiate a webdriver:
In [1]: from selenium import webdriver
In [2]: driver = webdriver.Chrome()
In [3]: driver.get('https://steemit.com/')
In [4]: print(driver.title)
Trending posts — Steemit
To avoid further selenium module usage confusion, please go through the "Get Started" section of the Python/Selenium documentation.
from your comment it looks like your code is:
import selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.chrome # should be webdriver.chrome()
driver.get('')
and your traceback is:
Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: module 'selenium.webdriver.chrome' has no attribute 'get'
which you are missing the instantiation of the driver, also as pointed out below chrome should be capitalised, so this:
driver = webdriver.chrome
needs to become:
driver = webdriver.Chrome() # the brackets mean the object is created or 'instantiated'

Find element and select option with phantomjs

I want to find an element by name and select an option from a drop-down menu by value with phantomjs. The following script
from selenium import webdriver
from selenium.webdriver.support.ui import Select
driver = webdriver.PhantomJS()
driver.get("http://www.wikipedia.org/")
select = Select(webdriver.find_element_by_name("language"))
select.select_by_value("es")
html_doc = driver.page_source
driver.quit()
generate the error
Traceback (most recent call last):
File "test.py", line 7, in <module>
select = Select(webdriver.find_element_by_name("language"))
AttributeError: 'module' object has no attribute 'find_element_by_name'
If I change webdriver.PhantomJS() I to webdriver.Firefox() I get the same error. What am I doing wrong? The module is not correctly installed?
webdriver is the module name you have imported, while driver is your WebDriver instance.
Change
select = Select(webdriver.find_element_by_name("language"))
^^^^^^^^^
to
select = Select(driver.find_element_by_name("language"))
^^^^^^

Categories