javascript - Getting selenium to send keys to google's sign in box (coding in python) - Stack Overflow

I have a problem with sending keys to the username and password fields in google's sign in box. Se

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.webdrivermon.keys import Keys
import time

username = "test"
password = "ninja"


driver = webdriver.Firefox()
driver.get(u'')

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 pany's customer's names (this is a business to business setup, so the customers are panies 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

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.mon.keys import Keys
import time

username = "test"
password = "ninja"


driver = webdriver.Firefox()
driver.get(u'http://www.google.')

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 pany's customer's names (this is a business to business setup, so the customers are panies 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

Share Improve this question edited Oct 31, 2012 at 18:36 Rookie asked Oct 31, 2012 at 17:00 RookieRookie 1,6005 gold badges21 silver badges35 bronze badges 1
  • @root thank you for your ments, I will edit the question accordingly. I've tried xpath. I can get the webelement, that's not the problem. I just can't send keys to it. – Rookie Commented Oct 31, 2012 at 17:41
Add a ment  | 

1 Answer 1

Reset to default 9

So this may be a bittersweet answer, but your script is essentially fine. Here is the problem:

username = "test"

#....

username = driver.find_element_by_id('Email')
username.send_keys(username)

You set username just fine, but you then go on to redefine it as the Email element, which then causes an error because of the final line - you are sending the username element back to itself as a string (which is the argument of send_keys), causing an Inception-like event of chaos. The len error is because Selenium is trying to take the length of the argument to send_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

user_field = driver.find_element_by_id('Email')
user_field.send_keys(username)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744375433a4571163.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信