using siblings in xpath, selenium ,webdriver

using siblings in xpath  to get the child / parent elements of the current element , can be used in relative xpath



xpath description
ancestor get all parent elements of current node
ancestor-or-self get all parent elements of current node and it self also
attribute get all attributes of the current node
child get all child elements of current node
descendant get all child elements and its child elements(grandchild) of current node
descendant-or-self get all child elements and its child elements(grandchild) of current node and it self
following get all elements in the html after this node
following-sibling get all siblings of the current node
parent get the parent of the current node


Example : the below code will get all the links in "Google" after searching for selenium in google.

List lst=driver.findElements(By.xpath("//*[@class='srg']/child::*"));
for(WebElement x : lst)
{
       System.out.println(x.findElement(By.tagName("a")).getText());
}

OR

List lst=driver.findElements(By.xpath("//*[@class='srg']/descendant::h3"));
for(WebElement x : lst)
{
       System.out.println(x.getText());

}














Comments