Selenium – My Learning’s – XPath

XPath: XPath, the XML Path Language, is a query language for selecting nodes from an XML document. In addition, XPath may be used to compute values (e.g., strings, numbers, or Boolean values) from the content of an XML document. XPath was defined by the World Wide Web Consortium. (Wikipedia)

I recorded one script now and here are the steps:

  1. Open www.flipkart.com
  2. Search for “Software Testing” books
  3. Click on the first search result (link)

The above script looks like – 

After running the above script I did not come across any errors, now I want to search for “Selenium Testing” and want to click on the first search result

Modified script looks like – 

Did you observe that, I have not changed the last line of the script, which says clickAndWait a link? Okay, let me run the script and see the result.

Wow!! It got PASSED without any errors. How it is possible? It is not pointing to the link name, whereas it is pointing to the Path of the link.

Then what we need to modify to make this script working. Add the Xpath of the link in the script. To find the Xpath I used Firebug add-on of Firefox (You can also use XPather add-on). Then I changed the script with Xpath (It looks like)

And yes, now my script is working as intended.

Do you have any other examples for Xpath, please do share with me.

Happy Learning – Happy Testing

Thanks,
Sudhamshu

 

Advertisements

3 thoughts on “Selenium – My Learning’s – XPath

  1. Hi Sudhamshu –

    From your question, it seems like you are interested in better using XPath for testing via Selenium. I’m a fellow computer professional, I just wrote a free tool that might help you use XPath better.

    Its called XPath AutoSuggest, it helps you write XPath by suggesting queries that match what you highlight in its editor. Just paste the source code of the page you are testing into my tool, highlight a node, for example, “Selenium Testing”, and it will generate several XPaths that will find your node. It is better than XPather in that regard because you don’t have to be very good at XPath.

    Again, it is free, there are no viruses (the source code is available for download so you can verify that). The tool is available on a Code Housing site you might have heard about, CodePlex, hosted by Microsoft.

    Here is the link https://xpathautosuggest.codeplex.com/.

    If you like it, maybe you could give me some stars on the CodePlex site, or even write a review.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s