python - selenium - 无法在 'evaluate' : The string is not a valid XPath expression 上执行 'Document'

标签 python python-3.x selenium xpath xpath-1.0

有如下页面。

http://remitly.com/us/en/

当您点击选择时,会出现一个国家列表。我尝试选择一个国家,例如哥伦比亚,然后单击它。但是我得到一个错误。

SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//span[contains(@class, 'md_countryName_fdxiah8' and text(), 'Colombia')]' is not a valid XPath expression.

select = driver.find_element_by_class_name('f1wrnyr7')
select.click()
countries = driver.find_element_by_class_name('f1o6pohl')
country = countries.find_element_by_xpath("//span[contains(@class, 'md_countryName_fdxiah8' and text(), 'Colombia')]")

最佳答案

这个错误信息...

SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//span[contains(@class, 'md_countryName_fdxiah8' and text(), 'Colombia')]' is not a valid XPath expression.

...暗示您使用的 XPath 不是有效的 XPath 表达式。

看来你们很接近。您可以使用以下任一项 Locator Strategies :

  • 使用 xpath 1:

    country = countries.find_element_by_xpath("//span[contains(@class, 'md_countryName_fdxiah8') and text()='Colombia']")
    
  • 使用 xpath 2:

    country = countries.find_element_by_xpath("//span[contains(@class, 'md_countryName_fdxiah8') and contains(., 'Colombia')]")
    

Here you can find a relevant discussion on SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//img[contains('1236548597')]' is not a valid XPath expression


更新

要克服 元素不可见 错误,您需要为 visibility_of_element_located() 引入 WebDriverWait 并且您可以使用以下任一定位策略:

element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[contains(@class, 'md_countryName_fdxiah8') and text()='Colombia']")))

关于python - selenium - 无法在 'evaluate' : The string is not a valid XPath expression 上执行 'Document',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57570395/

相关文章:

python-3.x - ServerSelectionTimeoutError : basicdatabase-w4eg3. mongodb.net:27017: [Errno 11001] getaddrinfo 失败

python - 在猎鹰中添加自定义 HTTP 方法?

python-3.x - 如何在 python 中打印 __init__ 函数参数?

python - 如何使用 Selenium 查找网页中元素的位置?

selenium - 在 docker 环境中使用 selenium 进行 Behat 测试

python - 无效的日期解析器

python - Paramiko SSH 连接错误 : socket. gaierror : [Errno 8] nodename nor servname provided, or not known

python - PyTorch找不到名字?? (名称错误: name 'device' is not defined)

python - 使用数组输入神经网络

ruby - 需要用 capybara(ruby) 在某个坐标点点击、按住并移动