11

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

Zaid
  • 35,070
  • 14
  • 81
  • 149
CColin
  • 139
  • 1
  • 1
  • 5

7 Answers7

3

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"]);
ashley
  • 519
  • 4
  • 11
2

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.

Oliver
  • 61
  • 3
2

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"]!;
mob
  • 110,546
  • 17
  • 138
  • 265
  • Thanks - but none of these worked... (and it isn't a curly quote either). So in the end I captured and looped through all span values, then used normal perl regex to match that to my string, as in: my $text = $sel->get_text() #insert XPath to span element in parantheses #use everyday perl to see if $text contains "It's a problem" – CColin Jun 28 '10 at 10:53
  • This worked great, since in chrome I needed to have quotes already around the entire xpath: $x("//div[.=\"My friend's value is here?\"]/../ul/li") – Charlie Seligman Sep 21 '18 at 14:45
1

Well the post is quite old. But here goes my working answer for those who still come wandering around looking for escaping single apostrophe and unable to find proper answer.

Text = It's a problem

Solution xpath = //div[text()=\"It's a problem\"]

or

Solution xpath = //div[contains(text(),\"It's a\")]
TheLethalCoder
  • 6,875
  • 6
  • 31
  • 58
Aditya
  • 67
  • 2
  • 10
0

Is it possible that the actual text on the web page is a curly quote and not a straight apostrophe? Also, you may have extra space at the beginning and end of the span, so that the strict equality against your string won't match.

Ned Batchelder
  • 323,515
  • 67
  • 518
  • 625
0

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']
Zaid
  • 35,070
  • 14
  • 81
  • 149
0

The solution to escaping apostrophes in xpath string literals is to double the apostrophe, e.g. qq(span[text()='It''s a problem'])