I have a problem with sending keys to the username and password fields in google’s sign in box. Selenium finds the webelements with the id’s “Email” and “Passwd”, but I cannot send any keys to them.
Here’s the code that isn’t yielding the expected results:
from selenium import webdriver
#from selenium.webdriver.common.keys import Keys
import time
username = "test"
password = "ninja"
driver = webdriver.Firefox()
driver.get(u'http://www.google.com')
driver.implicitly_wait(10)
elem = driver.find_element_by_id("gb_70")
elem.click()
username = driver.find_element_by_id('Email')
username.send_keys(username)
This code generates an error:
Traceback (most recent call last):
File “SERPkopi.py”, line 18, in
username.send_keys(username)
File “/Users/Sverdrup/virtualenv-1.6.1/Alert/lib/python2.7/site- packages/selenium/webdriver/remote/webelement.py”, line 142, in send_keys
local_file = LocalFileDetector.is_local_file(*value)
File “/xxxxx/xxxxxx/virtualenv-1.6.1/Alert/lib/python2.7/site- packages/selenium/webdriver/remote/webelement.py”, line 253, in is_local_file
for i in range(len(val)):
TypeError: object of type ‘WebElement’ has no len()
Which is strange, because the same code can write to google’s ‘q’ (query) field
I’ve tried to identify the webElement by id, name, xpath to no avail.
Background as to why:
I got my eyes open for google alerts today and want to set up alerts on my company’s customer’s names (this is a business to business setup, so the customers are companies themselves). The customers are relatively small, and I do not imagine that I’ll get a lot of alerts on their names, but it would be great to be able to keep track of them.
Seeing as how there isn’t a API for google alerts, I thought I’d use selenium to programmatically enter the couple of hundred customers names. I first have to be able to log on to my account though…
I would realy appreciate all and any help given.
Sincerely
So this may be a bittersweet answer, but your script is essentially fine. Here is the problem:
You set username just fine, but you then go on to redefine it as the
Emailelement, which then causes an error because of the final line – you are sending theusernameelement back to itself as a string (which is the argument ofsend_keys), causing an Inception-like event of chaos. Thelenerror is because Selenium is trying to take the length of the argument tosend_keys, which it expects to be a string but is in this case an element. In order to fix it, simply change one of the variable names. For instance