webdriver+python+selenium 登录163邮箱报错
from selenium import webdriverdriver = webdriver.Firefox()
driver.get("http://mail.163.com/")
driver.find_element_by_id("lbNormal").click()
driver.find_element_by_xpath("//form/div/div/div/input").send_keys("username")
driver.find_element_by_class_name("j-inputtext dlpwd").send_keys("password")
driver.find_element_by_id("dologin").click()
driver.close()
运行结果:
raceback (most recent call last):
File "D:\Python27\Test\autotest\mail163.py", line 6, in <module>
driver.find_element_by_xpath("//form/div/div/div/input").send_keys("username")
File "D:\Python27\lib\site-packages\selenium-2.53.1-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 290, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "D:\Python27\lib\site-packages\selenium-2.53.1-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 744, in find_element
{'using': by, 'value': value})['value']
File "D:\Python27\lib\site-packages\selenium-2.53.1-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 233, in execute
self.error_handler.check_response(response)
File "D:\Python27\lib\site-packages\selenium-2.53.1-py2.7.egg\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
NoSuchElementException: Message: Unable to locate element: {"method":"xpath","selector":"//form/div/div/div/input"}
Stacktrace:
at FirefoxDriver.prototype.findElementInternal_ (file:///c:/users/administrator/appdata/local/temp/tmphmt2c9/extensions/fxdriver@googlecode.com/components/driver-component.js:10770)
at FirefoxDriver.prototype.findElement (file:///c:/users/administrator/appdata/local/temp/tmphmt2c9/extensions/fxdriver@googlecode.com/components/driver-component.js:10779)
at DelayedCommand.prototype.executeInternal_/h (file:///c:/users/administrator/appdata/local/temp/tmphmt2c9/extensions/fxdriver@googlecode.com/components/command-processor.js:12661)
at DelayedCommand.prototype.executeInternal_ (file:///c:/users/administrator/appdata/local/temp/tmphmt2c9/extensions/fxdriver@googlecode.com/components/command-processor.js:12666)
at DelayedCommand.prototype.execute/< (file:///c:/users/administrator/appdata/local/temp/tmphmt2c9/extensions/fxdriver@googlecode.com/components/command-processor.js:12608)
我将代码中标红行改成通过id、name或class定位,都报找不到元素,请高手指点下,谢谢!
这个登录框有ifame,需要先切换ifame。 因为在你定位的用户名输入框元素节点上面有个iframe,所以要先进入找个iframe中,还有因为你的用户名和密码输入框元素ID是动态变化的,所以取ID会每次运行找不到元素,所以可以用name定位。如果你后面要定位的元素不在那个iframe下,需要跳出iframe,可以使用driver.switch_to_default_content()
参考如下:
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("http://mail.163.com/")
driver.find_element_by_id("lbNormal").click()
driver.switch_to_frame("x-URS-iframe")
driver.find_element_by_name("email").send_keys("username"))
driver.find_element_by_name("password").send_keys("password")
driver.find_element_by_id("dologin").click()
若尘_51 发表于 2016-9-22 08:35
这个登录框有ifame,需要先切换ifame。
嗯,谢谢! 掉渣饼 发表于 2016-9-22 09:31
因为在你定位的用户名输入框元素节点上面有个iframe,所以要先进入找个iframe中,还有因为你的用户名和密码 ...
非常感谢:handshake
页:
[1]