![](/img/trans.png)
[英]selenium - Failed to execute 'evaluate' on 'Document': The string is not a valid XPath expression
[英]SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//img[contains('1236548597')]' is not a valid XPath expression
我收到語法錯誤:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//img[contains('1236548597')]' is not a valid XPath expression.
我的代碼:
enter_chat = driver.find_element_by_xpath("//img[contains('1236548597')]")
enter_chat.click()
這個錯誤信息...
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//img[contains('1236548597')]' is not a valid XPath expression.
...暗示您使用的XPath不是有效的XPath表達式。
在您的代碼試驗中,帶有contains()
函數的謂詞有問題。
contains()
接受兩個參數。 第一個參數始終是要測試的屬性,第二個參數是要查找的值。 第一個參數應該是包含值1236548597的屬性:
enter_chat = driver.find_element_by_xpath("//img[contains(@<attributeName>,'1236548597')]")
如果沒有相關的 HTML,就很難構建一個答案。 然而,兩種可能的解決方案如下:
如果標簽是一個<a>
標簽,包含一個包含值1236548597的img
屬性:
enter_chat = driver.find_element_by_xpath("//a[contains(@img,'1236548597')]")
如果標簽是一個<img>
標簽,包含一個包含值1236548597的src
屬性:
enter_chat = driver.find_element_by_xpath("//img[contains(@src,'1236548597')]")
它應該是,
enter_chat = driver.find_element_by_xpath("//img[contains(text(),'1236548597')]")
enter_chat.click()
if "//img[contains(text(),'1236548597')]" not work,
try "//img[contains(.,'1236548597')]"
您錯過了 text() 函數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.