我有以下格式我試圖用硒/ Python來工作的標記屬性相匹配一個元素:Xpath的:基於相鄰元素
<tr>
<td><a href="www.google.com">google</a></td>
<td>useless text</td>
<td>useless text2</td>
<td>useless text3</td>
<td><a href="[email protected]">emailaddress</a></td>
</tr>
的想法是,給定一個已知的電子郵件地址(電子郵件地址td
中的href
的一部分),我可以在第一個td
中獲得(並單擊)a
。看起來xpath是用Selenium完成這個工作的最佳選擇。我想下面的XPath:
//*[@id="page_content"]/table/tbody/tr[2]/td[2]/div/table[1]/tbody/tr/td[4]/a[contains(@href, "mailto:[email protected]")]/../../td/a[0]
但我發現了這個錯誤:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"xpathhere"}
我知道的XPath來得到了「[email protected]」一個是正確的,因爲它只是從chrome開發工具中複製而來,所以在到達第一個a
元素後,錯誤必須與xpath的一部分相關。任何人都可以通過我的xpath瞭解一些問題嗎?
也許只是'// a [包含(@href,「[email protected]」)]'? – splash58
問題是我試圖檢索第一個'td'中的'a',所以我需要找出匹配「[email protected]」'a'後如何返回到那個級別。 – Marcatectura
你應該避免使用索引,總是使用你知道不會改變的東西,比如屬性的一部分,使用索引是一個不好的做法,並不是很靈活。 – lauda