泡沫666 发表于 2015-7-7 20:11:57

selenium自动化:为什么不能点击登录?

#coding=UTF-8
'''
Created on 2015-7-6

@author: paomo
'''

from selenium import webdriver

MyBrowser=webdriver.Firefox()
MyBrowser.get("https://www.baidu.com/")

MyBrowser.find_element_by_xpath("/html/body/div/div/div/div/a").click()

if __name__ == '__main__':
    pass

报错信息:
Traceback (most recent call last):
File "E:\Users\aidupeng\workspace\test\test1.py", line 14, in <module>
    MyBrowser.find_element_by_xpath("/html/body/div/div/div/div/a").click()
File "C:\Python27\lib\site-packages\selenium-2.40.0-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 221, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
File "C:\Python27\lib\site-packages\selenium-2.40.0-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 681, in find_element
    {'using': by, 'value': value})['value']
File "C:\Python27\lib\site-packages\selenium-2.40.0-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 164, in execute
    self.error_handler.check_response(response)
File "C:\Python27\lib\site-packages\selenium-2.40.0-py2.7.egg\selenium\webdriver\remote\errorhandler.py", line 164, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: u'Unable to locate element: {"method":"xpath","selector":"/html/body/div/div/div/div/a"}' ; Stacktrace:
    at FirefoxDriver.prototype.findElementInternal_ (file:///c:/users/aidupeng/appdata/local/temp/tmpjqsgdv/extensions/fxdriver@googlecode.com/components/driver_component.js:8904)
    at FirefoxDriver.prototype.findElement (file:///c:/users/aidupeng/appdata/local/temp/tmpjqsgdv/extensions/fxdriver@googlecode.com/components/driver_component.js:8913)
    at DelayedCommand.prototype.executeInternal_/h (file:///c:/users/aidupeng/appdata/local/temp/tmpjqsgdv/extensions/fxdriver@googlecode.com/components/command_processor.js:10878)
    at DelayedCommand.prototype.executeInternal_ (file:///c:/users/aidupeng/appdata/local/temp/tmpjqsgdv/extensions/fxdriver@googlecode.com/components/command_processor.js:10883)
    at DelayedCommand.prototype.execute/< (file:///c:/users/aidupeng/appdata/local/temp/tmpjqsgdv/extensions/fxdriver@googlecode.com/components/command_processor.js:10825)


joney2015 发表于 2015-7-7 22:42:28

xpath 路径请使用://html/body/div/div/div/div/a

joney2015 发表于 2015-7-7 22:43:08

请使用: //html/body/div/div/div/div/a

joykao 发表于 2015-7-8 10:51:24

xpath的值写错鸟,推荐款检测xpath的工具,操作见博客 http://anthonygao.blog.51cto.com/8653110/1418857
页: [1]
查看完整版本: selenium自动化:为什么不能点击登录?