文本查询中转义撇号

文本查询中转义撇号

本文介绍了如何使用 Perl 和 Selenium 在 XPath 文本查询中转义撇号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 XPath 查询,它需要匹配 span 属性中的一些文本,如下所示:

I have an XPath query which needs to match some text in a span attribute, as follows:

my $perl_query = qq(span[text\(\)='It's a problem']);

$sel->click_ok($perl_query);

文本没有撇号的地方没有问题.

Where the text has no apostrophe there is no problem.

我尝试了以下而不是这是一个问题":

I've tried the following instead of 'It's a problem':

'It\'s a problem'
'It&apos\;s a problem'
'It\${apos}s a problem'  #some thread on Stackoverflow suggested that this was a solution implemented by Selenium, but it doesn't work.

有什么想法吗?

另一方面,如果我不能解决这个问题,我会很高兴匹配问题",但不确定如何在 XPath 中使用 Selenium 进行正则表达式匹配.

On a different note, if I can't solve this, I'd be happy enough matching 'a problem' but not sure how to do regex matching in XPath with Selenium.

谢谢指点

推荐答案

这是一个 XPath 问题而不是 Perl 问题.

It's an XPath problem rather than the Perl problem.

这里已经详细讨论并回答了这个问题:http://kushalm.com/the-perils-of-xpath-expressions-specifically-escaping-quotes(断开的链接)

The problem was discussed and answered here in great detail:http://kushalm.com/the-perils-of-xpath-expressions-specifically-escaping-quotes (broken link)

简而言之,使用 concat()

my $perl_query = qq(span[text\(\)=concat("It","'","s a problem"]);

这篇关于如何使用 Perl 和 Selenium 在 XPath 文本查询中转义撇号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 08:51