简体   繁体   English

在硒python中使用xpath

[英]using an xpath in selenium python

I used firebug in Firefox to get a xpath for a link that did not have an ID assigned to it. 我在Firefox中使用firebug来获取未分配ID的链接的xpath。 The link is a javascript link with an image as the actual button. 该链接是一个以图片为实际按钮的javascript链接。 I'd like to be able to click this link but it's not working. 我希望能够单击此链接,但是它不起作用。

The actual xpath is '/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' 实际的xpath是'/ 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'

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()

I get the following error: is either invalid or does not result in a WebElement. The following error occurred:\\nInvalidSelectorError: Unable to locate an element with the xpath expression //*[@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 because of the following error:\\n[Exception... "The expression is not a legal expression." code: "12" nsresult: "0x805b0033 (SyntaxError)" location: "<unknown>"]' ; Stacktrace: 我收到以下错误: is either invalid or does not result in a WebElement. The following error occurred:\\nInvalidSelectorError: Unable to locate an element with the xpath expression //*[@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 because of the following error:\\n[Exception... "The expression is not a legal expression." code: "12" nsresult: "0x805b0033 (SyntaxError)" location: "<unknown>"]' ; Stacktrace: is either invalid or does not result in a WebElement. The following error occurred:\\nInvalidSelectorError: Unable to locate an element with the xpath expression //*[@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 because of the following error:\\n[Exception... "The expression is not a legal expression." code: "12" nsresult: "0x805b0033 (SyntaxError)" location: "<unknown>"]' ; Stacktrace:

<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"]')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM