selenium - 无法在“文档"上执行“评估":字符串不是有效的 XPath 表达式 [英] selenium - Failed to execute 'evaluate' on 'Document': The string is not a valid XPath expression
问题描述
有以下页面.
当您单击某个选择时,会出现一个包含国家/地区的列表.我尝试选择一个国家,例如哥伦比亚并单击它.但我收到一个错误.
<块引用>语法错误:无法在文档"上执行评估":字符串'//span[contains(@class, 'md_countryName_fdxiah8' and text(),'Colombia')]' 不是有效的 XPath 表达式.
select = driver.find_element_by_class_name('f1wrnyr7')选择.click()国家 = driver.find_element_by_class_name('f1o6pohl')country = countries.find_element_by_xpath("//span[contains(@class, 'md_countryName_fdxiah8' and text(), 'Colombia')]")
此错误信息...
语法错误:无法在文档"上执行评估":字符串 '//span[contains(@class, 'md_countryName_fdxiah8' and text(), 'Colombia')]' 不是有效的 XPath表达.
...暗示您使用的 XPath 不是有效的 XPath 表达式.
看来你很接近了.您可以使用以下任一定位器策略::>
使用 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')]")
在这里你可以找到关于语法错误:无法在文档"上执行评估":字符串//img[contains('1236548597')]"不是有效的 XPath 表达式
<小时>
更新
要克服元素不可见错误,您需要为 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']")))
There is the following page.
When you click on a select, a list with countries appears. I try to select one country, for example, Colombia and click on it. But I get an error.
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')]")
This error message...
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//span[contains(@class, 'md_countryName_fdxiah8' and text(), 'Colombia')]' is not a valid XPath expression.
...implies that the XPath which you have used was not a valid XPath expression.
Seems you were pretty close. You can use either of the following Locator Strategies:
Using xpath 1:
country = countries.find_element_by_xpath("//span[contains(@class, 'md_countryName_fdxiah8') and text()='Colombia']")
Using 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
Update
To overcome the element not visible error you need to induce WebDriverWait for visibility_of_element_located()
and you can use either of the following Locator Strategy:
element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[contains(@class, 'md_countryName_fdxiah8') and text()='Colombia']")))
这篇关于selenium - 无法在“文档"上执行“评估":字符串不是有效的 XPath 表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!