Build different XPATH expressions for the same HTML element

One of the difficult things to learn for testers that are new in test automation is selecting HTML elements in XPATH.

In order to click, select, type text in HTML elements, they need to be found first.

I have created a few different expressions that identify the same element of a page.

This is how to find the element matched in the next examples:

1. open www.vpl.ca

2. search for cats

3. identify the title link for the 7th result

Please click on the images from below to see them full screen.



//div[@id='bib1921954038']//span[@class='title']/a





/html/body/div[5]/div[7]/div/div/div[3]/form/div[3]/div/div[8]/div[2]/div/span[@class='title']/a




(//span[@class='title']/a)[last()-2]






(//span[@class='title']/a)[8]




//a[contains(@title, 'Book') and contains(@href, '1921954038_cats')]




All expressions do the same thing in different ways.

They are presented only to highlight the flexibility and power of XPATH.

From all 5 example expressions, the only one that I would use in my projects is the 3rd one.




_____________________________________________________________________________


Do you want to learn more about test automation and Java?
I will start an SELENIUM online group training on October 15.
Please see the training details here.

Share this