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.
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
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.
Hello gentlemen,
Thanks for your responses. I'm posting a specific problem below, and hopefully I'll get better at addressing these myself...
I'm on this webpage: http://mycorporation.intuit.com/corporation-llc-documents/
and I want to access the "Order Now" button under "Certificates of Good Standing" to the right.
This is the xpath provided by the XPather plugin: /html/body/div[@id='body']/div[@id='mainContent']/div[@id='mainMiddle']/div[@id='mainContentBody']/div[5]/div[2]/div[3]/a[1]/img
and I've tried to shorten it down to this: @browser.click "//a[contains(@href, 'certificateGS')]/img"
seems like it should work. but it doesn't. any help in explaining\rectifying the problem is much appreciated.
the command also works fine in IDE. just not in RC. i'm running it against IE7 and the script is written in Ruby on winxp sp2. i've verified the xpath in the XPath Checker plugin and the XPather as well. but to no avail. if anyone can provide the right command using css selectors instead of xpath, that would help as well.
I had been given the impression, when I first started looking at Selenium, that XPath is the way to go. So that's what I've tried to learn. Now the momentum seems to be with CSS. Anyway, I'd like to know how to access this particular element both ways. Please help!
thanks,
Viv.
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.
Alright, I think I've managed to resolve most of my issues. Full props to Patrick Lightbody, Adam Goucher and others on the IRC channel. Turns out my xpath was good enough for both Firefox and IE. Just that I happened to be using 1beta, and my issues got resolved when i switched to a more recent snapshot. Anyhow, I'd still like to know how to access my element above, using css selectors. Any input in this matter is much appreciated.
Thanks,
Viv.
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.
Hi Venkat,
Thanks for the input.
This is what worked for me...
browser.click("css=a[href*='certificateGS']")
Viv.
