SDET- QA Automation Techie

Software Testing Blog

  • Home
  • Training
    • Online
    • Self-Paced
  • Video Tutorials
  • Interview Skills
    • HR Interview Questions Videos
    • Domain Knowledge
  • Career Guidance
  • Home
  • Software Testing
    • Manual Testing Tutorials
    • Manual Testing Project
    • Manaul Testing FAQS
    • ISTQB
    • AGILE
  • Web Automation Testing
    • Java Programmng
    • Python Programmng
    • Selenium with Java
    • Selenium with Python
    • Robot Framework(Selenium with Python)
    • selenium with Cucumber
    • TestNG+IntelliJ
    • Mobile App Testing(Appium)
    • JMeter
  • API Automation Testing
    • Rest Assured API Testing (BDD)
    • Rest Assured API Testing (Java+ TestNG)
    • Robot Framework(Rest API Testing with Python)
    • Postman
    • SoapUI
    • API Testing(FAQ's)
  • SDET|DevOps
    • Continuos Integration
    • SDET Essentials
    • AWS For Testers
    • Docker
  • SQL
    • Oracle(SQL)
    • MySQL for Testers
    • NoSQL
  • Unix/Linux
    • UNIX TUTORIALS
    • Linux Shell Scripting
  • ETL Testing
    • ETL Data warehouse Tutorial
    • ETL Concepts Tools and Templates
    • ETL Testing FAQ's
    • ETL Testing Videos
  • Big Data Hadoop
  • Video Tutorials
  • ApachePOI Video Tutorials
  • Downloads
    • E-Books for Professionals
    • Resumes
  • Automation Essencials
    • Cloud Technologies
      • Docker For Testers
      • AWS For Testers
      • Sub Child Category 3
    • Java Collections
    • Selenium Locators
    • Frequently Asked Java Programs
    • Frequently Asked Python Programs
    • Protractor
    • Cypress Web Automation

Selenium Locators - XPath Axes




XPath axes are those axes that are used to search for the multiple nodes in the XML document from the current node context.
These methods are mainly used when the web element is not identified with the help of ID, name, class name, link text, CSS selector and XPath, etc. locators.

  • Self
  • Parent
  • Child
  • Ancestor
  • Descendant
  • Following
  • Following-sibling
  • Preceding
  • preceding-sibling




Example:

import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class LocatorsDemo5_XPAthAxes {

public static void main(String[] args) {

System.setProperty("webdriver.chrome.driver","C://Drivers//chromedriver_win32//chromedriver.exe");
WebDriver driver=new ChromeDriver();

driver.get("https://money.rediff.com/gainers/bse/daily/groupa");
driver.manage().window().maximize();

//Self  - Selects the current node
String text=driver.findElement(By.xpath("//a[contains(text(),'India Tourism De')]/self::a")).getText();
System.out.println(text); //India Tourism De


//Parent - Selects the parent of the current node (always One)
text=driver.findElement(By.xpath("//a[contains(text(),'India Tourism De')]/parent::td")).getText();
System.out.println(text);  //India Tourism De

//Child - Selects all children of the current node (One or many)
List childs=driver.findElements(By.xpath("//a[contains(text(),'India Tourism De')]/ancestor::tr/child::td"));
System.out.println("Number of child elements:"+childs.size());//5

//Ancestor - Selects all ancestors (parent, grandparent, etc.) 
text=driver.findElement(By.xpath("//a[contains(text(),'India Tourism De')]/ancestor::tr")).getText();
System.out.println(text);

//Descendant - Selects all descendants (children, grandchildren, etc.) of the current node
List descendants=driver.findElements(By.xpath("//a[contains(text(),'India Tourism De')]/ancestor::tr/descendant::*"));
System.out.println("Number of descendant nodes:"+descendants.size());

//Following -Selects everything in the document after the closing tag of the current node
Listfollowingnodes=driver.findElements(By.xpath("//a[contains(text(),'India Tourism De')]/ancestor::tr/following::tr"));
System.out.println("Number of following nodes:"+followingnodes.size());

//Following-sibling : Selects all siblings after the current node
List followingsiblings=driver.findElements(By.xpath("//a[contains(text(),'India Tourism De')]/ancestor::tr/following-sibling::tr"));
System.out.println("Number of Following Siblings:"+followingsiblings.size());

//Preceding - Selects all nodes that appear before the current node in the document
List precedings=driver.findElements(By.xpath("//a[contains(text(),'India Tourism De')]/ancestor::tr/preceding::tr"));
System.out.println("Number of preceding nodes:"+precedings.size());

//preceding-sibling - Selects all siblings before the current node
List precedingsiblings=driver.findElements(By.xpath("//a[contains(text(),'India Tourism De')]/ancestor::tr/preceding-sibling::tr"));
System.out.println("Number of preceding sibling nodes:"+precedingsiblings.size());

driver.close();
}

}

Hands on:
1) Find the sign-up button from the registration form present in the Facebook application. (child)
//div[@id='reg_form_box']/child::div[10]/button

2) Locate Firstname field from SignUp button in facebook (Parent)
//button[@id='u_0_13']//parent::*/parent::*/child::div[1]/div[1]/div[2]
//button[@id='u_0_13']//ancestor::div[2]/child::div[1]/div[1]/div[2]


3) Identify the Password from Mobile number filed in facebook.(Following)
//input[@id='u_0_r']/following::input[2]


4) Locate Mobile number from newpassword field in facebook (preceding)
//input[@id='password_step_input']/preceding::input[2]


5) Locate surname from female radio button in facebook(Ancestor)
//input[@id='u_0_6']/ancestor::div[2]/div[1]/div[1]/div[2]
//input[@id='u_0_6']/ancestor::div[2]//input[@id='u_0_o']

6) Identify the search text box from the Google search button present in the Google search home page. (Parent)
//div[@class='FPdoLc tfB0Bf']//input[@name='btnK']/parent::*/parent::*/parent::*/div[1]


7) Identify the Today’s Deals link from the amazon search text box present in the amazon home page.(Following)
//input[@id='twotabsearchtextbox']/following::a[contains(text(),'Best Sellers')]

8) Identify the Hello, Signin from the amazon search text box present in the amazon home page. (Following)
//input[@id='twotabsearchtextbox']/following::span[contains(text(),'Hello, Sign in')]

9) Identify Mobiles link which is part of Menu bar - Amazon  (Descendant)
//div[@id='nav-xshop']/descendant::a[1]
//div[@id='nav-xshop']/descendant::a[contains(text(),'Mobiles')]

  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to TwitterShare to Facebook
Newer Post Older Post Home
popup

Popular Posts

  • How To Explain Project In Interview Freshers and Experienced
    “ Describe an important project you’ve worked on ” is one of the most common questions you can expect in an interview. The purpose of a...
  • MANUAL TESTING REAL TIME INTERVIEW QUESTIONS & ANSWERS
    1. How will you receive the project requirements? A. The finalized SRS will be placed in a project repository; we will access it fr...
  • API/Webservices Testing using RestAssured (Part 1)
    Rest Assured : Is an API designed for automating REST services/Rest API's Pre-Requisites Java Free videos: https://www.you...

Facebook Page

Pages

  • Home
  • Resumes
  • Job Websites India/UK/US
  • ISTQB
  • Selenium with Java
  • E-Books for Professionals
  • Manual Testing Tutorials
  • Agile Methodology
  • Manual Testing Projects

Live Traffic

YouTube


Blog Visitors

Copyright © SDET- QA Automation Techie | Powered by Blogger
Design by SDET | Blogger Theme by | Distributed By Gooyaabi Templates