今天玩了一下python 下的selenium,感觉蛮好玩的。试试看能不能模拟新浪微博登录。
模仿了一下官网给的例子试了一下,代码如下:
-
from selenium import webdriver
-
from selenium.webdriver.common.keys import Keys
-
-
driver = webdriver.Firefox()
-
driver.get("")
-
-
elem=driver.find_element_by_name("username")
-
elem.send_keys("name")
-
-
elem2 = driver.find_element_by_name("password")
-
elem2.send_keys("passwd")
-
elem2.send_keys(Keys.RETURN)
结果发现出现如下错误:
selenium.common.exceptions.ElementNotVisibleException: Message: u'Element is not currently visible and so may not be interacted with' ; Stacktrace:
at fxdriver.preconditions.visible (file:///tmp/tmpoOAZdm/extensions/fxdriver@googlecode.com/components/command_processor.js:8791)
at DelayedCommand.prototype.checkPreconditions_ (file:///tmp/tmpoOAZdm/extensions/fxdriver@googlecode.com/components/command_processor.js:11438)
at DelayedCommand.prototype.executeInternal_/h (file:///tmp/tmpoOAZdm/extensions/fxdriver@googlecode.com/components/command_processor.js:11455)
at DelayedCommand.prototype.executeInternal_ (file:///tmp/tmpoOAZdm/extensions/fxdriver@googlecode.com/components/command_processor.js:11460)
at DelayedCommand.prototype.execute/< (file:///tmp/tmpoOAZdm/extensions/fxdriver@googlecode.com/components/command_processor.js:11402)
原因:
-
elem=driver.find_element_by_name("username")
这行代码得到的elem并不是唯一的,
改成如下就好:
-
elem = driver.find_element_by_xpath("//div[@class='W_login_form']/div[@class='info_list']/div/input")
阅读(1853) | 评论(0) | 转发(0) |