Published September 17, 2020 by

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')]

Read More
    email this
Published September 17, 2020 by

Selenium Locators - XPath



XPath is defined as XML path.
It is a syntax or language for finding any element on the web page using XML path expression. 
XPath is used to find the location of any element on a webpage using HTML DOM structure.
XPath can be used to navigate through elements and attributes in DOM. 

In this article we will disccuss
1) What is XPath?
2) What is DOM?
3) Types of XPath ( Absolute & Relative)
4) Diff between Absolute & Relative xpaths 
5) How to capture XPaths - Extensions for chrome
6) Which XPath is preferred? Why?
7) XPath options
  •    or
  •    and
  •    contains()
  •    starts-with()
  •    text()
  •    chained xpath



Example:
-----------------

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class LocatorsDemo4_XPaths {

public static void main(String[] args) {

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

WebDriver driver=new ChromeDriver();

driver.get("http://automationpractice.com/index.php");
driver.manage().window().maximize();

//Absolute xpath
//driver.findElement(By.xpath("/html[1]/body[1]/div[1]/div[1]/header[1]/div[3]/div[1]/div[1]/div[2]/form[1]/input[4]")).sendKeys("T-shirts");
//driver.findElement(By.xpath("/html[1]/body[1]/div[1]/div[1]/header[1]/div[3]/div[1]/div[1]/div[2]/form[1]/button[1]")).click();

//Relative xpath
//driver.findElement(By.xpath("//input[@id='search_query_top']")).sendKeys("T-shirts");
//driver.findElement(By.xpath("//button[@name='submit_search']")).click();


//xpath with   'or'

//driver.findElement(By.xpath("//input[@id='search_query_top' or @name='search_queryX']")).sendKeys("T-shirts");
//driver.findElement(By.xpath("//button[@name='submit_searchX' or @class='btn btn-default button-search']")).click();

//xpath with 'and'
//driver.findElement(By.xpath("//input[@id='search_query_top' and @name='search_query']")).sendKeys("T-shirts");
//driver.findElement(By.xpath("//button[@name='submit_search' and @class='btn btn-default button-search']")).click();

//xpath with contains()
//driver.findElement(By.xpath("//input[contains(@id,'query_top')]")).sendKeys("T-shirts"); // id=search_query_top
//driver.findElement(By.xpath("//button[contains(@name,'search')]")).click(); // name=submit_search

//xpath with start-with()
//driver.findElement(By.xpath("//input[starts-with(@id,'search_query')]")).sendKeys("T-shirts"); // id=search_query_top
//driver.findElement(By.xpath("//button[starts-with(@name,'submit_')]")).click(); // name=submit_search

//xpath with text()
//driver.findElement(By.xpath("//a[text()='Women']")).click();   // WOMEN Tab

//chained xpath

driver.findElement(By.xpath("//form[@id='searchbox']//input[@id='search_query_top']")).sendKeys("T-shirts");
driver.findElement(By.xpath("//form[@id='searchbox']//button[@name='submit_search']")).click();


}

}

Read More
    email this
Published September 17, 2020 by

Selenium Locators - Id, Name, LinksText, partial LinkText & CSS Selector




Locators in Selenium are one of the most powerful commands. Its ideally the building block of the Selenium automation scripts. It helps locate the GUI elements through which multiple user actions can be performed. These are one of the important parameters for scripting, and if they end up to be incorrect or brittle, they may lead to script failure. A good scripting base foundation requires elements to be located appropriately. For this, we have multiple locators in Selenium WebDriver. Below is the list of these locators of Selenium WebDriver :
  • Id
  • Name
  • Link Text
  • Partial LinkText
  • ClassName
  • Tag Name
  • CSS Selector
  • XPath



Example 1:
---------------------------------------------
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class LocatorsDemo1 {

public static void main(String[] args) {

System.setProperty("webdriver.chrome.driver","C:\\Drivers\\chromedriver_win32\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.get("http://automationpractice.com/index.php");

driver.manage().window().maximize(); // maximize web page


// id & name locators
WebElement searchbox=driver.findElement(By.id("search_query_top"));
searchbox.sendKeys("T-shirts");

driver.findElement(By.name("submit_search")).click();

//linkText & partial linkText
//driver.findElement(By.linkText("Printed Chiffon Dress")).click();
driver.findElement(By.partialLinkText("Chiffon Dress")).click();

}

}

Example 2:
---------------------------------------------
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class LocatorsDemo2 {

public static void main(String[] args) {

System.setProperty("webdriver.chrome.driver","C:\\Drivers\\chromedriver_win32\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.get("http://automationpractice.com/index.php");

driver.manage().window().maximize(); // maximize web page

//className
int sliders=driver.findElements(By.className("homeslider-container")).size();
System.out.println(sliders);

//TagName
int links=driver.findElements(By.tagName("a")).size();
System.out.println(links);
}

}

Example3
-------------------------------------
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class LocatosDemo3 {

public static void main(String[] args) {

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

driver.get("https://www.facebook.com/");
driver.manage().window().maximize(); // maximize web page

//Tag & ID
//driver.findElement(By.cssSelector("input#email")).sendKeys("David");
//driver.findElement(By.cssSelector("#email")).sendKeys("David");

// Tag & Class
//driver.findElement(By.cssSelector("input.inputtext")).sendKeys("John");
//driver.findElement(By.cssSelector(".inputtext")).sendKeys("John");

//Tag & attribute
//driver.findElement(By.cssSelector("[name=email]")).sendKeys("Smith");
//driver.findElement(By.cssSelector("input[name=email]")).sendKeys("Smith");

//Tag , class & attribute
driver.findElement(By.cssSelector("input.inputtext[data-testid=royal_email]")).sendKeys("Smith");
driver.findElement(By.cssSelector("input.inputtext[data-testid=royal_pass]")).sendKeys("abc");

}

}

Read More
    email this