【问题标题】:SyntaxError: Failed to execute 'evaluate' on 'Document': The string 'xpath' is not a valid XPath expression [duplicate]SyntaxError:无法在“文档”上执行“评估”:字符串“xpath”不是有效的 XPath 表达式 [重复]
【发布时间】:2021-02-19 11:12:24
【问题描述】:

当我尝试使用以下 xpath 查找元素时,出现语法错误,这可能是什么原因,我正在关注这个问题,但它对我不起作用

https://stackoverflow.com/a/58400631/12849200

我的 Xpath:

dp_month = driver.find_element_by_xpath('//*/td[@aria-label='+month_label+']/div[contains(text(),'+ x_month +')]')
selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpath expression //*/td[@area-label=November 2025]/div[contains(text(),NOV)] because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//*/td[@area-label=November 2025]/div[contains(text(),NOV)]' is not a valid XPath expression.

【问题讨论】:

    标签: selenium selenium-webdriver


    【解决方案1】:

    如果您查看错误消息中的 XPath,您可能会发现问题所在。您的文本没有按要求用引号括起来,例如

    td[@area-label=November 2025]
    

    应该是

    td[@area-label="November 2025"]
    

    要解决此问题,您需要将代码行调整为

    dp_month = driver.find_element_by_xpath('//*/td[@aria-label="'+month_label+'"]/div[contains(text(),"'+ x_month +'")]')
    

    【讨论】:

      猜你喜欢
      • 2019-12-25
      • 2019-02-03
      • 2021-08-30
      • 1970-01-01
      • 1970-01-01
      • 2018-10-07
      • 1970-01-01
      • 2015-05-08
      • 2011-06-03
      相关资源
      最近更新 更多