Selenium
XSLT Report in Selenium Webdriver
XSLT Report The XSLT Report in the Selenium framework is a very important feature that is used to...
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 –
Let's study them in detail -
contains() in Selenium is a function within Xpath expression which is used to search for the web elements that contain a particular text. We can extract all the elements that match the given text value using the XPath contains() function throughout the webpage. Contains in XPath has ability to find the element with partial text.
Ex. Here we are searching an anchor .contains text as 'SAP M'.
"//h4/a[contains(text(),'SAP M')]"
NOTE: You can practise the following XPath exercise on this http://demo.gtupapers.com/test/selenium-xpath.html
A Sibling in Selenium Webdriver is a function used to fetch a web element which is a sibling to the parent element. If the parent element is known then the web element can be easily found or located that can use the sibling attribute of the Xpath expression in selenium webdriver.
Sibling in XPath 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 gtupapers has been updated so use the demo site instead to run tests
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.gecko.driver", driverPath);
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://demo.gtupapers.com/test/gtupapershome/");
//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:
Ancestor in Selenium Webdriver is a function used to find the ancestor of a specific member at the specified layer. The level of ancestor to be returned or level of the ancestor relative to level of the member can be explicitly specified. It returns number of hierarchical steps from the ancestor, locating the specified ancestor that user wants.
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"
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.firefox.FirefoxDriver;
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.gtupapers.com/test/gtupapershome/");
//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-
By using AND and OR you can put 2 conditions in our XPath expression.
Here our XPath query will be like
Xpath=//*[@type='submit' OR @name='btnReset']
Xpath=//input[@type='submit' and @name='btnLogin']
Test Steps :
You will find an element using AND and OR, parent, starts-with, and XPath axes
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class AND_OR {
public static void main(String[] args) {
WebDriver driver;
WebElement w,x;
System.setProperty("webdriver.chrome.driver","E://Selenium//Selenium_Jars//chromedriver.exe");
driver= new ChromeDriver();
// Launch the application
driver.get("https://www.gtupapers.com/");
//Search element using OR in the xpath
w=driver.findElement(By.xpath("//*[@type='submit' OR @name='btnReset']"));
//Print the text of the element
System.out.println(w.getText());
//Search element using AND in the xpath
x=driver.findElement(By.xpath("//input[@type='submit' and @name='btnLogin']"));
//Print the text of the searched element
System.out.println(x.getText());
//Close the browser
driver.quit();
}
}
Parent in Selenium is a method used to retrieve the parent node of the current node selected in the web page. It is very useful in the situation when you select an element and need to get the parent element using Xpath. This method is also used to get the parent’s parent.
Here our XPath query will be like
Xpath=//*[@id='rt-feature']//parent::div
XPath using Parent
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class Parent {
public static void main(String[] args) {
WebDriver driver;
WebElement w;
System.setProperty("webdriver.chrome.driver","E://Selenium//Selenium_Jars//chromedriver.exe");
driver= new ChromeDriver();
// Launch the application
driver.get("https://www.gtupapers.com/");
//Search the element by using PARENT
w=driver.findElement(By.xpath("//*[@id='rt-feature']//parent::div"));
//Print the text of the searched element
System.out.println(w.getText());
//Close the browser
driver.quit();
}
}
Using Starts-with function, you can find the element whose attribute dynamically changes on refresh or other operations like click, submit, etc.
Here our XPath query will be like
Xpath=//label[starts-with(@id,'message')]
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class StartsWith {
public static void main(String[] args) {
WebDriver driver;
WebElement w;
System.setProperty("webdriver.chrome.driver","E://Selenium//Selenium_Jars//chromedriver.exe");
driver= new ChromeDriver();
// Launch the application
driver.get("https://www.gtupapers.com/");
//Search the element by using starts-with
w=driver.findElement(By.xpath("//label[starts-with(@id,'message')]"));
//Print the text of the searched element
System.out.println(w.getText());
//Close the browser
driver.quit();
}
}
By using XPath axes, you can find the dynamic and very complex elements on a web page. XPath axes contain several methods to find an element. Here, will discuss a few methods.
following: This function will return the immediate element of the particular component.
Here our XPath query will be like
Xpath=//*[@type='text']//following::input
XPath using following
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class Following {
public static void main(String[] args) {
WebDriver driver;
WebElement w;
System.setProperty("webdriver.chrome.driver","E://Selenium//Selenium_Jars//chromedriver.exe");
driver= new ChromeDriver();
// Launch the application
driver.get("https://www.gtupapers.com/");
//Search the element by using Following method
w=driver.findElement(By.xpath("//*[@type='text']//following::input"));
//Print the text of the searched element
System.out.println(w.getText());
//Close the browser
driver.quit();
}
}
Preceding: This function will return the preceding element of the particular element.
Here our XPath query will be like
Xpath= //*[@type='submit']//preceding::input
XPath using Preceding
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class Preceding {
public static void main(String[] args) {
WebDriver driver;
WebElement w;
System.setProperty("webdriver.chrome.driver","E://Selenium//Selenium_Jars//chromedriver.exe");
driver= new ChromeDriver();
// Launch the application
driver.get("https://www.gtupapers.com/");
//Search the element by using preceding method
w=driver.findElement(By.xpath("//*[@type='submit']//preceding::input"));
//Print the searched element
System.out.println(w.getText());
//Close the browser
driver.quit();
}
}
d) Descendant: This function will return the descendant element of the particular element.
Here our XPath query will be like
Xpath= //*[@id='rt-feature']//descendant::a
XPath using Descendant
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class Descendant {
public static void main(String[] args) {
WebDriver driver;
WebElement w;
System.setProperty("webdriver.chrome.driver","E://Selenium//Selenium_Jars//chromedriver.exe");
driver= new ChromeDriver();
// Launch the application
driver.get("https://www.gtupapers.com/");
//Search the element by using descendant method
w=driver.findElement(By.xpath("//*[@id='rt-feature']//descendant::a"));
//Print the searched element
System.out.println(w.getText());
//Close the browser
driver.quit();
}
}
XSLT Report The XSLT Report in the Selenium framework is a very important feature that is used to...
We will use the Mercury Tours website as our web application under test. It is an online flight...
To understand how to run scripts in parallel, let's first understand Why do we need Session...
What is Gecko Driver? The term Gecko stands for a Web Browser engine that is inbuilt within...
Project Summary This project will put you in an online Corporate Test Environment. You will be...
In this Selenium vs UFT tutorial, we are going to compare very popular automation tools - QTP vs...