!!

Welcome to Selenium Webdriver Forum

As a web user you can read and search through forum content. Once you have registered and signed you can post topics, post replies, set up watch lists, modify forum settings, and use private messaging.

Author Topic: Single quote inside Xpath expression not working  (Read 1859 times)

nagaraja javali

  • Web User
Single quote inside Xpath expression not working
« on: May 03, 2012, 02:59:10 PM »
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

  • Web User
Re: Single quote inside Xpath expression not working
« Reply #1 on: May 03, 2012, 02:59:32 PM »
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
 
On Apr 2, 11:27 pm, nagaraja javali <nagajav...@gmail.com> wrote:
 

- Show quoted text -

Paul Grandjean

  • Web User
Re: Single quote inside Xpath expression not working
« Reply #2 on: May 03, 2012, 02:59:47 PM »
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
 

- Show quoted text -

Selenium Webdriver

Re: Single quote inside Xpath expression not working
« Reply #2 on: May 03, 2012, 02:59:47 PM »

Hien Ngo

  • Web User
Re: Single quote inside Xpath expression not working
« Reply #3 on: May 03, 2012, 03:00:03 PM »
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
 

- Show quoted text -

Tarun Kumar

  • Web User
Re: Single quote inside Xpath expression not working
« Reply #4 on: May 03, 2012, 03:00:19 PM »
I doubt if "pure" css has a way to use text matching like -
 
<< contains(text() >>
 

- Show quoted text -

nagaraja javali

  • Web User
Re: Single quote inside Xpath expression not working
« Reply #5 on: May 03, 2012, 03:00:36 PM »
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
 
On Apr 3, 10:10 pm, Mike Riley <lvskip...@cox.net> wrote:
 

- Show quoted text -

Mike Riley

  • Web User
Re: Single quote inside Xpath expression not working
« Reply #6 on: May 03, 2012, 03:00:54 PM »
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
 

- Show quoted text -

 

Related Topics

  Subject / Started by Replies Last post
4 Replies
1906 Views
Last post April 01, 2012, 06:28:29 PM
by NeelakandanRVJ
2 Replies
913 Views
Last post April 01, 2012, 07:03:29 PM
by Mark Collin
2 Replies
1197 Views
Last post April 01, 2012, 11:20:39 PM
by Mark Collin
1 Replies
746 Views
Last post July 25, 2012, 03:39:11 PM
by dlai
1 Replies
1860 Views
Last post October 11, 2012, 11:50:33 PM
by shivas.cit
2 Replies
5405 Views
Last post September 12, 2012, 02:47:16 AM
by divya.madaan
0 Replies
351 Views
Last post December 01, 2012, 04:38:00 AM
by zubinix