7 Replies Last post: Jan 5, 2009 11:14 PM by Vivek Netha  
Vivek Netha Beginner 4 posts since
Jan 1, 2009
Currently Being Moderated

Jan 1, 2009 3:11 PM

How to write good XPath for Selenium

Hello,

 

I've been a Watir user for the past couple of years;

and have just recently started to try and switch over to Selenium.

mainly for cross-platform compatibility.

 

But I'm having the worst possible time with getting XPath right.

Even the simplest script I attempt to write breaks down when I run it in RC.

 

--> I've tried the the XPath recorded by IDE

--> I've tried the XPath Checker plugin

--> I've tried Firebug

 

None of them work properly.

 

Is there a good tool that generates XPath which can be copied right over to a Selenium Script, without any modifications whatsoever?

Or is there a book I can read, or a handy tutorial with actual examples that explains how to write XPath for several different kinds of DOM elements??

 

Please help!

 

 

Viv.

Tarun Master 380 posts since
Apr 28, 2008
Currently Being Moderated
1. Jan 1, 2009 10:17 PM in response to: Vivek Netha
Re: How to write good XPath for Selenium

My suggestion to you is to avoid using Xpath. Instead use id, name if available.

Xpath is browser specific and may work for one browser and not for other. (This is my understanding of Xpath)

Apropos of reading material for Xpath, I guess Google should help you here.

 

~ Tarun K

pvtrao Pro 105 posts since
Oct 24, 2008
Currently Being Moderated
2. Jan 2, 2009 6:23 AM in response to: Tarun
Re: How to write good XPath for Selenium

Hi vivek,

Xpaths are evaluating differently for different browsers.  We have to constructing an Xpath expression which will work on all the browsers.  If you want to test your application on different browsers using selenium please use the Xpath in an optimized way.  sometimes it also give some performance issues while evaluating in the html hierarchy.

 

So try to avoid Xpath as much as possible.

In this case CSS selectors are very useful to wrok with multiple browsers.  Use firebug to get the element description and use any property and value pair.  It is very easy to identify the element uniquely.

 

See other posts on CSS Selectors.

 

Thanks,

Venkat.

Santiago Suarez Ordoñez Hero 508 posts since
Aug 7, 2008
Currently Being Moderated
4. Jan 2, 2009 9:33 PM in response to: Vivek Netha
Re: How to write good XPath for Selenium

1f1yOF-25i-e8b

 

Maybe the problem is caused because your application is giving a

different HTML code to IE. You should use IE developer toolbar

(Microsoft's poor copy of firebug) to see the source code given and

check the XPath is still correct.

 

pvtrao Pro 105 posts since
Oct 24, 2008
Currently Being Moderated
6. Jan 5, 2009 3:16 AM in response to: Vivek Netha
Re: How to write good XPath for Selenium

Hi,

 

The CSS selector for the OrderNow button under Certificates of Good Standing

css=div.infoColumn:contains("Obtain Certified Documents") > div:nth-child(3) > div.buttonsDiv img


please see my other posts on CSS selectors for more info.

 

Thanks,

Venkat.

More Like This

  • Retrieving data ...