【问题标题】:How to find xpath when we have single inverted comma in our html Ex: ```sortable=" 'name' "```当我们的 html 中有单引号时如何找到 xpath 例如:```sortable=" 'name' "```
【发布时间】:2017-08-31 13:24:38
【问题描述】:

当我们的 html 中有单引号时如何找到 xpath-

<td class="ng-binding" sortable="'name'" data-title="'Department Name'" data-title-text="Department Name">dept1</td>

我想在上面提到的类和可排序的基础上找到xpath。

我尝试过的代码-

//td[@class='ng-binding'][@sortable=''name''] 

但它给了我错误。

任何有价值的帮助。 如果有任何澄清,请告诉我。

【问题讨论】:

标签: java selenium xpath selenium-chromedriver


【解决方案1】:

试试//td[@class='ng-binding'][@sortable="'name'"]

问题在于,通过使用 2 个连续的单引号,它被解释为好像您传入了一个空字符串,然后在关闭标签之前在它后面加上了 name'',这是完全无效的。

或者像这样逃跑:

"//td[@class='ng-binding'][@sortable='\'name\'']"

【讨论】:

  • 您的 xpath 根据我的查询工作,但是当我在脚本中使用它时,它显示错误“无效字符常量”
  • 使用Shubnam的风格,也可以转义需要的'.添加到答案。
  • 无法通过此定位路径。
  • 提供一些关于如何使用 XPath 的信息,也许那里出了点问题。
【解决方案2】:

试试:-

"//td[@name='\'name'\'"

"//td[@name=\"'name\'"

参考:- https://sqa.stackexchange.com/questions/26341/how-to-write-xpath-if-i-have-apostrophe-in-my-xpath-element

希望对你有帮助:)

【讨论】:

  • 无法通过此定位路径。
【解决方案3】:

这会定位元素,现在如果有多个“1”符合这些条件,它将失败。所以添加一个索引“可能”是必要的。或者你需要使用不同的条件。

//td[@class='ng-binding'][contains(@sortable, 'name')][text()='dept1']

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-04
    • 1970-01-01
    • 2016-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-22
    • 1970-01-01
    相关资源
    最近更新 更多