【问题标题】:How to use single quotation marks inside a xpath expression in Selenium?如何在 Selenium 的 xpath 表达式中使用单引号?
【发布时间】:2019-07-07 18:27:16
【问题描述】:

我需要在网站上搜索一个名称可能包含撇号的按钮,但我不知道该怎么做。

a = "Ming'an Cui"
n = "55"
driver.find_element_by_xpath("//button[contains(span,'"+a+"')]/span[@class='btn-subtext' and contains(text(),"+n+")]").click()

然后我收到这条消息:

InvalidSelectorException: Message: Given xpath expression "//button[contains(span,'Ming'an Cui')]/span[contains(span,'Ming'an Cui')]" is invalid: SyntaxError: The expression不是合法的表达方式。

【问题讨论】:

  • 你能提供那个按钮的html例子吗?
  • 按钮>
  • //button/span[@class='btn-subtext' and contains(text(),"+n+")]
  • 谢谢,但有多个按钮的编号相同。
  • //button[span[text()='name here'] 和 span[text()='number here']] 试试这个,用你的值替换 name 和 numbers

标签: python selenium xpath


【解决方案1】:

我相信你必须在back slash (\)之前使用escape sequence character 下面提到的一些事情

driver.find_element_by_xpath("//button[contains(span,'"+"Ming\'an Cui"+"')]/span[@class='btn-subtext' and contains(text(),"+n+")]").click()

【讨论】:

  • InvalidSelectorException: Message: Given xpath expression "//button[contains(span,'Ming'an Cui')]/span[@class='btn-subtext' and contains(text(), 55)]" 无效:SyntaxError: The expression is not a legal expression.
【解决方案2】:
driver.find_element_by_xpath('//button[contains(span,\"'+a+'\")]/span[@class="btn-subtext" and contains(text(),"55")]').click()#nomes

修复它:D

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-13
    • 2016-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多