有如下页面。
当您点击选择时,会出现一个国家列表。我尝试选择一个国家,例如哥伦比亚,然后单击它。但是我得到一个错误。
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/