Sunday, May 8, 2011

Xpath - relatives

Recently I wanted to use Xpath to verify two objects in the page exist. Without having to write a few lines of code, not saying that might be a better idea but for the sake of learning new tricks with Xpath here is how we verify two elements with a parent-child or ancestor-child relationship.

Two objects:

 1) vlad's shop
 2) PayPal and Credit Cards

Because the two elements we are looking for have different relationships in the node we have to use "AxisName" (ancestor\parent\child).

This xpath verifies that we are on "Vlad's Page" and the page contains the credit cards accepted image.

//img[@alt='PayPal and Credit Cards']/ancestor::* //a[contains(@href,"top_trail")]