简体   繁体   English

Selenium XPath文本搜索在substring中使用单引号

[英]Selenium XPath text search with single quote in substring

My html code will be like 我的HTML代码就像

<td class="summaryValRt2 summaryValDetails">Appledore Engineering v Shaw's - Dan Day_220217103442</td>

I use below xpath expression to click element operation. 我使用下面的xpath表达式来单击元素操作。

//*[@class='summaryValRt2 summaryValDetails'][text()='Appledore Engineering v Shaw's - Dan Day_220217103442']

result: 结果:

unable to click element due to non visibility of element 由于元素不可见而无法点击元素

.

I checked it problem due to ' character in text [text()='Appledore Engineering v **Shaw's** - Dan Day_220217103442'] . 由于'文字中的字符[text()='Appledore Engineering v **Shaw's** - Dan Day_220217103442']我检查了它的问题。

can any one share your thoughts? 任何人都可以分享你的想法吗?

In XPath 2.0, you can escape the quote with a backslash: 在XPath 2.0中,您可以使用反斜杠转义引号:

text()='Appledore Engineering v Shaw\'s - Dan Day_220217103442'

However, most major browsers do not support XPath 2.0 at this time. 但是,目前大多数主流浏览器都不支持XPath 2.0。 Until they do, you're limited to the XPath 1.0 spec, which does not support that escape character. 在他们这样做之前,你只能使用XPath 1.0规范,它不支持该转义字符。

Since your string includes only single quotes, you could instead wrap the string in double quotes: 由于您的字符串仅包含单引号,因此您可以将字符串换成双引号:

text()="Appledore Engineering v Shaw's - Dan Day_220217103442"

That will cover most cases, but it gets trickier if you're searching for a string that includes both single quotes and double quotes. 这将涵盖大多数情况,但如果您正在搜索包含单引号双引号的字符串,则会变得更加棘手。

For example, suppose you are searching for the following string: 例如,假设您要搜索以下字符串:

single'quote double"quote

You can achieve that using concat with the alternating quotes trick: 您可以使用带有交替引号技巧的concat来实现这一点:

text()=concat("single'quote", ' double"quote')

Try with [text()='Appledore Engineering v Shaw\\'s - Dan Day_220217103442'] , I used escape sequence in this example. 试试[text()='Appledore Engineering v Shaw\\'s - Dan Day_220217103442'] ,我在这个例子中使用了转义序列

Or you could try with unicode code for single quote, something like [text()='Appledore Engineering v Shaw\'s - Dan Day_220217103442'] 或者您可以尝试单引号的unicode代码,例如[text()='Appledore Engineering v Shaw\'s - Dan Day_220217103442']

请使用下面的xpath,它正常工作给我。

//*[@class='summaryValRt2 summaryValDetails'][text()="Appledore Engineering v Shaw's - Dan Day_220217103442"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM