Python Selenium SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//div[contains(@class, 'product-card__subtitle')
Python Selenium SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//div[contains(@class, 'product-card__subtitle')
正在尝试获取 div class 的 XPath 和 div 中的文本。两个 div 是:
<div class="product-card__subtitle">Women's Shoe</div>
<div class="product-card__subtitle">Men's Shoe</div>
我给出 selenium 错误的 Xpath 是:
driver.find_element_by_xpath("//div[contains(@class, 'product-card__subtitle') and text("
")='Women's Shoe']")
driver.find_element_by_xpath("//div[contains(@class, 'product-card__subtitle') and text()='Men's "
"Shoe']")
我正在尝试获取“女鞋”部分的路径和“男鞋”部分的另一条路径
根本原因是文本有符号'
。
虽然您可以优化其他 xpath 语法表达式,但您不必这样做。
编辑 1:
driver.find_element_by_xpath("//div[contains(@class, 'product-card__subtitle') and text()=\"Women's Shoe\"]")
driver.find_element_by_xpath("//div[contains(@class, 'product-card__subtitle') and text()=\"Men's Shoe\"]")
我认为下面是正确的,但是在 chrome 控制台中验证时,我仍然得到错误“不是有效的 XPath 表达式”
driver.find_element_by_xpath("//div[contains(@class, 'product-card__subtitle') and text()='Women\'s Shoe']")
driver.find_element_by_xpath("//div[contains(@class, 'product-card__subtitle') and text()='Men\'s Shoe']")
这个错误信息...
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//div[contains(@class, 'product-card__subtitle')
...表示您使用的 XPath 不是有效的 XPath 表达式.
解决方案
要找到所需的元素,您可以使用以下 xpath based :
Women's Shoe
:
driver.find_element_by_xpath("//div[@class='product-card__subtitle' and contains(., \"Women's Shoe\")]")
Men's Shoe
:
driver.find_element_by_xpath("//div[@class='product-card__subtitle' and contains(., \"Men's Shoe\")]")
参考资料
您可以在以下位置找到一些相关的详细讨论:
正在尝试获取 div class 的 XPath 和 div 中的文本。两个 div 是:
<div class="product-card__subtitle">Women's Shoe</div>
<div class="product-card__subtitle">Men's Shoe</div>
我给出 selenium 错误的 Xpath 是:
driver.find_element_by_xpath("//div[contains(@class, 'product-card__subtitle') and text("
")='Women's Shoe']")
driver.find_element_by_xpath("//div[contains(@class, 'product-card__subtitle') and text()='Men's "
"Shoe']")
我正在尝试获取“女鞋”部分的路径和“男鞋”部分的另一条路径
根本原因是文本有符号'
。
虽然您可以优化其他 xpath 语法表达式,但您不必这样做。
编辑 1:
driver.find_element_by_xpath("//div[contains(@class, 'product-card__subtitle') and text()=\"Women's Shoe\"]")
driver.find_element_by_xpath("//div[contains(@class, 'product-card__subtitle') and text()=\"Men's Shoe\"]")
我认为下面是正确的,但是在 chrome 控制台中验证时,我仍然得到错误“不是有效的 XPath 表达式”
driver.find_element_by_xpath("//div[contains(@class, 'product-card__subtitle') and text()='Women\'s Shoe']")
driver.find_element_by_xpath("//div[contains(@class, 'product-card__subtitle') and text()='Men\'s Shoe']")
这个错误信息...
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//div[contains(@class, 'product-card__subtitle')
...表示您使用的 XPath 不是有效的 XPath 表达式.
解决方案
要找到所需的元素,您可以使用以下 xpath based
Women's Shoe
:driver.find_element_by_xpath("//div[@class='product-card__subtitle' and contains(., \"Women's Shoe\")]")
Men's Shoe
:driver.find_element_by_xpath("//div[@class='product-card__subtitle' and contains(., \"Men's Shoe\")]")
参考资料
您可以在以下位置找到一些相关的详细讨论: