Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I escape an apostrophe in my XPath text query with Perl and Selenium?

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

like image 732
CColin Avatar asked Jun 26 '10 17:06

CColin


3 Answers

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

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

In a nutshell, modify your xquery to assemble the quote-containing string using concat()

my $perl_query = qq(span[text\(\)=concat("It","'","s a problem"]);
like image 111
ashley Avatar answered Oct 13 '22 08:10

ashley


A couple of suggestions; hopefully at least one of them will work:

my $perl_query = qq!span[text()='It\\'s a problem']!;
my $perl_query = qq!span[text()="It's a problem"]!;
like image 28
mob Avatar answered Oct 13 '22 09:10

mob


I just had the same problem and google didn't give me a satisfied solution.

I tried to substring this: value=' - ending with an Apostrophe.

My XPath that works look like:

"substring-after(., concat('value=', ''''))"

So four Apostrophes in a row.

like image 40
Oliver Avatar answered Oct 13 '22 10:10

Oliver