Single quote inside Xpath expression not working

8,754 views
Skip to first unread message

nagaraja javali

unread,
Apr 3, 2012, 2:27:12 AM4/3/12
to Selenium Users
Hi,

In Xpath single quote(') inside xpath function contains() is not
working for me. Below is xpath:

selenium.type(“//table[@id='table']/tbody/tr/td[contains(text(),'abc's
10')]/following-sibling::td/input”,"text1");

I need to type the text 'text1' in the input field based on row
contains text "abc's 10' But the above command gives the "Invalid
Xpath' error. I have also tried the below alternatives to escape the
single quote.

1) selenium.type(“//table[@id='table']/tbody/tr/td[contains(text(),'abc
\'s 10')]/following-sibling::td/input”,"text1");

//slash(\) to escape single quote

2) selenium.type(“//table[@id='table']/tbody/tr/td[contains(text(),'abc
\\'s 10')]/following-sibling::td/input”,"text1");

3)selenium.type(“//table[@id='table']/tbody/tr/td[text()='abc\'s 10']/
following-sibling::td/input”,"text1");


All the above types are giving the same error. I tried the below one
to avoid single quote:

selenium.type(“//table[@id='table']/tbody/tr/td[contains(text(),'
10')]/following-sibling::td/input”,"text1");

//Using part of the text "10"

This works fine and the commands executes. But i think this is not the
proper solution, I wonder why the single quote(,) inside two single
quotes is not working in xpath?.

Thanks and Regards,
Nagaraj


Mike Riley

unread,
Apr 3, 2012, 1:10:48 PM4/3/12
to Selenium Users
Try this variation (using backslahes can be tricky, eh?):
selenium.type("//table[@id='table']/tbody/tr/td[text()=\"abc's 10\"]/
following-sibling::td/input","text1");

I wouldn't trust the backslashes to be passed down correctly for the
XPath to interpret them the way you were trying to do, because Java is
trying to interpret them at compile time. By using the backslash to
simply put "abc's 10" inside double quotes as part of the string I
think you will be okay. I do this lots of places where I am writing
out HTML using Report.log() to my report HTML file for TestNG.

Mike

Paul Grandjean

unread,
Apr 3, 2012, 1:44:50 PM4/3/12
to seleniu...@googlegroups.com
Please make sure you realize using css selectors is the preferred way over xpath for most of the selenium community.  css selectors run faster and are less susceptible to behavior differences among browsers.

Paul


--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en.


Hien Ngo

unread,
Apr 3, 2012, 6:12:28 PM4/3/12
to seleniu...@googlegroups.com

Hi:
I am trying to learn css, can someone help me by converting the xpaths in the 1st email in this chain to css?
Thanks

Tarun Kumar

unread,
Apr 4, 2012, 12:47:41 AM4/4/12
to seleniu...@googlegroups.com
I doubt if "pure" css has a way to use text matching like - 

<< contains(text() >>
To post to this group, send email to selenium-users@googlegroups.com.
To unsubscribe from this group, send email to selenium-users+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en.

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to selenium-users@googlegroups.com.
To unsubscribe from this group, send email to selenium-users+unsubscribe@googlegroups.com.

nagaraja javali

unread,
Apr 4, 2012, 11:19:19 AM4/4/12
to Selenium Users
Hi Mike,

I tried selenium.type("//table[@id='table']/tbody/tr/td[text()=\"abc's
10\"]/
and it is working fine. Thank you very much.

Regards,
Nagaraj

Mike Riley

unread,
Apr 4, 2012, 1:08:25 PM4/4/12
to seleniu...@googlegroups.com
Passing something you escape with a backslash from something that also uses backslash to escape special characters is always tricky.  I try to make it so that I only have to use it at the highest level.  Luckily JavaScript, and hence XPath, can use either single or double quotes, so that seemed like the best way in your case.

I was a bit surprised that option 2 didn't work for you, too.

Mike
Reply all
Reply to author
Forward
0 new messages