Selenium ‘nonetype’ object has no attribute ‘send_keys’
title='this is the title'
I want to use python/selenium positioning, in the web page, this line:
<input id="subject" name="subject" maxlength="50" value="" class="nude error" type="text">
I used this code with python-selenium (under Debian:
title = driver.find_element_by_id("subject").clear()
title.send_keys(title)
I get the following error:
Traceback (most recent call last):
File "./basic0", line 49, in <module>
titre.send_keys(title)
AttributeError: 'NoneType' object has no attribute 'send_keys'
Note: When the script stops because of this error, the mouse cursor is in the correct position in the web page; But I can’t find send_keys to fill in the input
I also tried:
title = driver.find_element_by_xpath("div[contains(text(),'subject')]")
title = driver.find_element_by_xpath("//form[input/@id='subject']")
title = driver.find_element_by_xpath("//form[input[@name='subject']")
But it doesn’t work; Additionally, the mouse cursor is not in the correct position.
Then I tried the later Selenium version:
I completely cleared the python-selenium package under Debian (i.e. selenium v. 2.53)
And then
pip install selenium==3.3.1
This time, when I start the script, it says that the geckodriver is missing:
So,
wget https://github.com/mozilla/geckodriver/releases/download/v0.23.0/geckodriver-v0.23.0-linux32.tar.gz
tar -xvzf geckodriver-v0.23.0-linux32.tar.gz
chmod 755 geckodriver (I also tried 777)
mv geckodriver /usr/local/bin/ (so it's in my PATH)
Now when I start the script, this is the error message I get :
Traceback (most recent call last):
File "./basic0", line 13, in <module>
driver = webdriver. Firefox()
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.py", line 155, in __init__
keep_alive=True)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 92, in __init__
self.start_session(desired_capabilities, browser_profile)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 179, in start_session
response = self.execute(Command.NEW_SESSION, capabilities)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 238, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 193, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: connection refuse
The Firefox window pops up and then closes when the script stops
Solution
You have assigned a method call (clear())
that returns None
to the title
variable, and you need to define WebElement and call the method as follows
title = driver.find_element_by_id("subject")
title.clear()
title.send_keys("title")