Java 使用webdriver查找元素时如何在xpath中使用撇号(')?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37542773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use apostrophe (') in xpath while finding element using webdriver?
提问by dolittle
I need to use apostrophe (') in my xpath expression which i need to use in finding elements using webdriver
我需要在我的 xpath 表达式中使用撇号 ('),我需要使用它来使用 webdriver 查找元素
i need to use below Xpath expression
我需要使用下面的 Xpath 表达式
//input[@text="WE'd like to hear from you"]
while using the above expression in find elements function i am replacing double quotes with single quotes
在查找元素函数中使用上述表达式时,我正在用单引号替换双引号
driver.findelements(By.xpath("//input[@text='WE'd like to hear from you']"))
采纳答案by k.s. Karthik
Use the xpath as shown below:
使用 xpath 如下所示:
driver.findElements(By.xpath("//input[contains(@text,\"WE'd\")]"));
Hope this helps.
希望这可以帮助。
回答by har07
You have to use double-quotes as your XPath string literal delimiter, since XPath 1.0 doesn't provide a way of escaping quotes. In addition to that, you can escape the double-quotes in Java, to avoid it from conflicting with your Java string delimiter, which also use double-quotes :
您必须使用双引号作为XPath 字符串文字定界符,因为 XPath 1.0 不提供转义引号的方法。除此之外,您可以转义 Java 中的双引号,以避免它与您的Java 字符串分隔符冲突,后者也使用双引号:
driver.findelements(By.xpath("//input[@text=\"WE'd like to hear from you\"]"))
回答by Moin
The Escape character usage does not serve the purpose. I tried the concatenation function and it worked like a charm. Please see the below xpath.
转义字符的使用不能达到目的。我尝试了连接功能,它的作用就像一个魅力。请参阅下面的 xpath。
tag: li Manager of Workflow Initiator's Manager /li
标签: li Manager of Workflow Initiator's Manager /li
Concatenate function and split the string as –
连接函数并将字符串拆分为 –
concat('Manager of Workflow Initiator',"'",'s Manager')
Single quoteis kept in double quotewhile other characters are kept in single quotes..
单引号保留在双引号中,而其他字符保留在单引号中。
So XPath looks as –
所以 XPath 看起来像 –
//li[.=concat('Manager of Workflow Initiator',"'",'s Manager')]
回答by Moin
if the above solution doesn't work then use the below solution using escape sequence.
如果上述解决方案不起作用,则使用以下使用转义序列的解决方案。
xpath: //li[.=\"Manager of Workflow Initiator's Manager\"]
Here we are treating the whole text as a string using the escape character
在这里,我们将整个文本视为使用转义字符的字符串
回答by 3ven
I Encountered a similar situation where I need to write an xpath for an element shown below:
我遇到了类似的情况,我需要为如下所示的元素编写 xpath:
Element:
元素:
<img src="webwb/pzspacer.gif!!.gif" class="inactvIcon" data-ctl="["DatePicker"]" style="cursor:pointer;">
I was able to grep the element using below Xpath, where I used the backslash to escape the characters [ and ".
我能够在 Xpath 下面使用 grep 元素,在那里我使用反斜杠来转义字符 [ 和 "。
Xpath: //img[@data-ctl='\[\"DatePicker\"\]']
路径://img[@data-ctl='\[\"DatePicker\"\]']
Hope this helps.
希望这可以帮助。