【问题标题】:Xpath doesn't work with string, which have quotes. ChromeXpath 不适用于带有引号的字符串。铬合金
【发布时间】:2021-04-24 02:39:03
【问题描述】:

我有文字: var1 = 'ТСЖ "ВОЛГОГРАДСКИЙ ПР-Т 50-1, 52-1, 52-2, 56-1, 56-2, 58-1, 58-3, 60-2, 64-1, 64-2, 66-2, ВОЛЖСКИЙ 21"'

我有 xpath: driver.find_element_by_xpath("//a[text()='"+var1+"']/ancestor::div[contains(@class,'chakra-stack') and contains(@class,'css-1oap1wr')]/following-sibling::div/descendant::button[text()='Отклонить']").click() - 不行

//a[text()='ТСЖ "ВОЛГОГРАДСКИЙ ПР-Т 50-1, 52-1, 52-2, 56-1, 56-2, 58-1, 58-3, 60-2, 64-1, 64-2, 66-2, ВОЛЖСКИЙ 21"']/ancestor::div[contains(@class,'chakra-stack') and contains(@class,'css-1oap1wr')]/following-sibling::div/descendant::button[text()='Отклонить'] - 但是这个工作!

Python 给我写信:selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//a[text()='ТСЖ "ВОЛГОГРАДСКИЙ ПР-Т 50-1, 52-1, 52-2, 56-1, 56-2, 58-1, 58-3, 60-2, 64-1, 64-2, 66-2, ВОЛЖСКИЙ 21"']/ancestor::div[contains(@class,'chakra-stack') and contains(@class,'css-1oap1wr')]/following-sibling::div/descendant::button[text()='Отклонить']"}

但是,如果我从错误消息中剪切 xpath 并将其像 xpath 一样放在 Chrome 的控制台中 - 会找到元素。

元素绝对可见且已加载。 对不起我的英语。

【问题讨论】:

  • 您需要正确转义引号。您的问题/代码有点难以阅读,所以我无法给出确切的答案。

标签: python selenium google-chrome selenium-chromedriver quotes


【解决方案1】:

您需要转义引号。试试这个:

var1 = 'ТСЖ "ВОЛГОГРАДСКИЙ ПР-Т 50-1, 52-1, 52-2, 56-1, 56-2, 58-1, 58-3, 60-2, 64-1, 64-2, 66-2, ВОЛЖСКИЙ 21"'

driver.find_element_by_xpath('//a[text()=\''+var1+'\']/ancestor::div[contains(@class,\'chakra-stack\') and contains(@class,\'css-1oap1wr\')]/following-sibling::div/descendant::button[text()=\'Отклонить\']').click()

或者这个

var1 = 'ТСЖ "ВОЛГОГРАДСКИЙ ПР-Т 50-1, 52-1, 52-2, 56-1, 56-2, 58-1, 58-3, 60-2, 64-1, 64-2, 66-2, ВОЛЖСКИЙ 21"'

#replace the double quotes with \"
var1 = var1.replace('"', '\\"')
driver.find_element_by_xpath("//a[text()='"+var1+"']/ancestor::div[contains(@class,'chakra-stack') and contains(@class,'css-1oap1wr')]/following-sibling::div/descendant::button[text()='Отклонить']").click()

【讨论】:

  • 我做到了,它也不起作用。如果我在 xpath 中替换 " on \" 它看起来像 \"
  • 我不明白你的评论。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-02-07
  • 2019-01-31
  • 2018-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多