[英]using an xpath in selenium python
我在Firefox中使用firebug來獲取未分配ID的鏈接的xpath。 該鏈接是一個以圖片為實際按鈕的javascript鏈接。 我希望能夠單擊此鏈接,但是它不起作用。
實際的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()
我收到以下錯誤: 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.