How do I escape an apostrophe in my XPath text que

2020-07-05 06:08发布

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.

Any ideas?

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.

Thanks for any pointers

7条回答
▲ chillily
2楼-- · 2020-07-05 06:46

Consider breaking up your string if possible:

my $spanValue = q/text()='It's a problem'/;
my $perlQuery = qq/span[$spanValue]/;

# $perlQuery = span[text()='It's a problem']
查看更多
登录 后发表回答