Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在selenium python中使用xpath_Python_Python 2.7_Selenium_Xpath_Selenium Webdriver - Fatal编程技术网

在selenium python中使用xpath

在selenium python中使用xpath,python,python-2.7,selenium,xpath,selenium-webdriver,Python,Python 2.7,Selenium,Xpath,Selenium Webdriver,我在Firefox中使用firebug为未分配ID的链接获取xpath。该链接是一个javascript链接,图像作为实际按钮。我希望能够单击此链接,但它不起作用 实际的xpath是'/html/body/div[2]/div/div/div[3]/div/div/div/table/tbody/tr[1]/td[2]/form/table/tbody/tr[1]/td/div/table/thead/tr[2]/th[1]/a/img' from selenium import webdriv

我在Firefox中使用firebug为未分配ID的链接获取xpath。该链接是一个javascript链接,图像作为实际按钮。我希望能够单击此链接,但它不起作用

实际的xpath是'/html/body/div[2]/div/div/div[3]/div/div/div/table/tbody/tr[1]/td[2]/form/table/tbody/tr[1]/td/div/table/thead/tr[2]/th[1]/a/img'

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()

URL = 'http://example.com'

driver.set_window_size(1024, 768)
driver.get(url)

link = driver.find_element_by_xpath(//*[@id="//html/body/div[2]/div/div/div[3]/div/div/‌​table/tbody/tr[1]/td[2]/form/table/tbody/tr[1]/td/div[1]/div/table/thead/tr[2]/th‌​[1]/a/img"])
for link in links:
 link.click()
我得到以下错误:
无效或未生成WebElement。出现以下错误:\nInvalidSelectorError:无法找到xpath表达式为//*[@id=/html/body/div[2]/div/div/div[3]/div/div/table/tbody/tr[1]/td[2]/form/table/tbody/tr[1]/td/div/table/thead/tr[2]/th[1]/a/img的元素,因为以下错误:\n[异常…”该表达式不是合法表达式。“代码:“12”nsresult:“0x805b0033(语法错误)”位置:”];堆栈跟踪:

<a onclick="if(typeof functionx == 'function'){functionx(document.getElement…,'liststuff','');}return false" href="#">

   <img style="border: 0px;" onmouseover="this.src='/add-hover.gif';" onmouseout="this.src='/add.gif';" alt="Add" src="/icon-add.gif"></img>

</a>

依赖元素的父级和属性,而不是元素的绝对路径:

link = driver.find_element_by_xpath('//a[@onclick and @href = "#" and img/@alt = "Add"]')

依赖元素的父级和属性,而不是元素的绝对路径:

link = driver.find_element_by_xpath('//a[@onclick and @href = "#" and img/@alt = "Add"]')

您可以显示包含链接的相关html代码,或者提供网站的链接吗?谢谢。@alecxe很抱歉,我无法提供url,因为它位于公司的内部服务器上。猜测:
标记可能具有
img
属性,而不是嵌套的
标记。如果是这样,请尝试将
a/img
更改为
a/@img
在XPath的末尾。@user2242044您的XPath非常脆弱。请提供一个html代码,以便我们可以看到那里的链接。@alecxe我在原始文章中添加了元素,但我无法添加完整的html代码。您能显示包含链接的相关html代码吗,或者提供到网站的链接吗?谢谢。@alecxe很抱歉,我无法提供url,因为它位于一家公司的内部服务器上。
标记可能具有
img
属性,而不是嵌套的
标记。如果是这样,请尝试在XPath末尾将
a/img
更改为
a/@img
。@user2242044您拥有的XPath非常脆弱。请提供一个html code,这样我们就可以在那里看到链接。@alecxe我在原始文章中添加了元素,但无法添加完整的html代码。现在执行的代码将出现问题,但不会打开手动单击链接时打开的页面。@user2242044我已更新了xpath表达式,请检查。结果仍然相同。没有错误,但没有打开link.PyCharm给出了以下结果
过程结束,退出代码为0
这起作用了!我意外删除了
链接。单击()
line。非常感谢。标记为最佳答案。这将立即执行代码问题,但不会打开手动单击链接时打开的页面。@user2242044我已更新了xpath表达式,请检查。结果仍然相同。没有错误,但未打开链接。PyCharm给出以下结果
过程结束,退出code 0
成功了!我不小心删除了
链接。单击()
行。非常感谢。标记为最佳答案。