XPath Contains, Sibling, Ancestor Functions in Selenium WebDriver
If a simple XPath is not able to find a complicated web element for our test script, we need to use the functions from XPath 1.0 library. With the combination of these functions, we can create more specific XPath. Let's discuss a 3 such functions –
- Contains
- Sibiling
- Ancestor
Let's study them in detail -
Contains: By using 'contains' function in XPath, we can extract all the elements which matches a particular text value.
Ex. Here we are searching an anchor .contains text as 'SAP M'.
"//h4/a[contains(text(),'SAP M)']"
Sibling: Using sibling keyword, we can fetch a web element on the which is related to some other element.
Example: Here on the basis of sibling element of 'a' we are finding 'h4'
"//div[@class='canvas- graph']//a[@href='/accounting.html'][i[@class='icon-usd']]/following-sibling::h4"
Ancestor: To find an element on the basis of the parent element we can use ancestor attribute of XPath.
Lets understand these 3 functions using an example –
Test Steps
Note: Since the date of creation of tutorial the Homepage of Guru99 has been updated so use the demo site instead to run tests
- Go to http://demo.guru99.com/selenium/guru99home/
- In the section 'A few of our most popular courses', search all Web Elements which are sibling of a WebElement whose text is 'SELENIUM'
- We will find element using contains , ancestor and sibling function
USING Contains and Sibling
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
public class SiblingAndParentInXpath {
@Test
public void testSiblingAndParentInXpath(){
WebDriver driver;
String driverPath = "C:\\geckodriver.exe";
System.setProperty("webdriver.firefox.marionette", driverPath);
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://demo.guru99.com/selenium/guru99home/");
//Search element inside 'Popular course' which are sibling of control 'SELENIUM' ,Here first we will find a h2 whose text is ''A few of our most popular courses' ,then we move to its parent element which is a 'div' , inside this div we will find a link whose text is 'SELENIUM' then at last we will find all of the sibling elements of this link('SELENIUM')
List >WebElement< dateBox = driver.findElements(By.xpath("//h2[contains(text(),'A few of our most popular courses')]/parent::div//div[//a[text()='SELENIUM']]/following-sibling::div[@class='rt-grid-2 rt-omega']"));
//Print all the which are sibling of the the element named as 'SELENIUM' in 'Popular course'
for (WebElement webElement : dateBox) {
System.out.println(webElement.getText());
}
driver.close();
}
}
Output will be like:
Using Ancestor function
We can achieve the same functionality with the help of a function 'ancestor' as well.
Now suppose we need to Search All elements in 'Popular course' section with the help of ancestor of the anchor whose text is 'SELENIUM'
Here our xpath query will be like
"//div[.//a[text()='SELENIUM']]/ancestor::div[@class='rt-grid-2 rt-omega']/following-sibling::div"
Complete Code
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
public class AncestorInXpath{
@Test
public void testAncestorInXpath(){
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://demo.guru99.com/selenium/guru99home/");
//Search All elements in 'Popular course' section
//with the help of ancestor of the anchor whose text is 'SELENIUM'
List<WebElement> dateBox = driver.findElements(By.xpath("//div[.//a[text()='SELENIUM']]/ancestor::div[@class='rt-grid-2 rt-omega']/following-sibling::div"));
//Print all the which are sibling of the element named as 'SELENIUM' in 'Popular course'
for (WebElement webElement : dateBox) {
System.out.println(webElement.getText());
}
driver.quit();
}
}
Output will look like-
Summary:
- There are some situation when regular xpath cannot be used to find element. In these situation we need different functions from xpath query.
- There some important xpath functions like contains, parent, ancestors, following-sibling etc.
- With the help of these functions we can create complex xpath expressions.






Thank you for sharing such kind of precious information with us.It really useful for many of them like me.
ReplyDeleteSoftware Testing Training in Chennai
Software Training Institutes in Chennai
JAVA Training in Chennai
Python Training in Chennai
Hadoop Training in Chennai
Selenium Training in Chennai
Software Testing Training in Chennai
Software testing training in T Nagar