【问题标题】:Unable to make the xpath work with escapes无法使 xpath 与转义一起使用
【发布时间】:2017-09-14 12:36:24
【问题描述】:

我目前正在使用 xpath 来检查 id 和占位符是否匹配:

我已经编写了这个 xpath,但是我遇到了转义问题。我不知道如何处理这个

self.driver.find_element_by_xpath("//input[@id="searchvalue" and @placeholder="Searched for: L1-Multi-level Combo   contains \'Corporate\General Counsel's Office (GCO)\'"]").click()

这就是我的 HTML 代码的样子:

<input type="text" name="searchvalue" id="searchvalue" placeholder="Searched for: L1-Multi-level Combo   contains 'Corporate\General Counsel's Office (GCO)\'" label="Search" title="Searched for: L1-Multi-level Combo   contains 'Corporate\General Counsel's Office (GCO)\'" class="uneditable-inputRight searchtext" onkeypress="if((event&amp;&amp;event.which==13)||(window.event&amp;&amp;window.event.keyCode==13)){quickSearchArcadia(gbappid, false, gbentity);}" onfocus="showSearchOptions()">

【问题讨论】:

  • 试试'//input[@id="searchvalue" and @placeholder="Searched for: L1-Multi-level Combo contains \'Corporate\General Counsel\'s Office (GCO)\'"]'
  • 有什么问题?
  • @Andersson,它不起作用。我添加了我的 HTML 代码截图
  • 你最好提供文本,而不是图片
  • 您可以尝试将转义字符串存储为变量,然后只传递变量,尽管我不认为这是“正确”的解决方案。

标签: python selenium


【解决方案1】:

通常使用id 属性应该足以定位所需元素,因为它必须是唯一值

self.driver.find_element_by_id("searchvalue")

但如果您还想使用placeholder 属性,请尝试以下表达式:

self.driver.find_element_by_xpath('//input[@id="searchvalue" and @placeholder="Searched for: L1-Multi-level Combo   contains \'Corporate\\General Counsel\'s Office (GCO)\\\'"]')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多