0% found this document useful (0 votes)
6 views2 pages

XPath Selenium WebDriver Java Guide

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

XPath Selenium WebDriver Java Guide

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

XPath in Selenium WebDriver (Java) - Beginner to Expert

1. Introduction to XPath

XPath is used in Selenium to locate elements in the DOM of web pages. It?s especially useful for elements without

unique identifiers.

2. Setup for Selenium Java

Import necessary packages and set up your driver:

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

public class XPathExamples {

public static void main(String[] args) {

WebDriver driver = new ChromeDriver();

driver.get("https://example.com");

// Examples here...

3. Amazon.in ? Search iPhone

driver.get("https://www.amazon.in/");

driver.findElement(By.xpath("//input[@id='twotabsearchtextbox']")).sendKeys("iPhone");

driver.findElement(By.xpath("//input[@id='nav-search-submit-button']")).click();

driver.findElement(By.xpath("(//span[contains(text(),'iPhone')])[1]")).click();

4. Facebook.com ? Login

driver.get("https://www.facebook.com/");

driver.findElement(By.xpath("//input[@id='email']")).sendKeys("testuser");
XPath in Selenium WebDriver (Java) - Beginner to Expert

driver.findElement(By.xpath("//input[@id='pass']")).sendKeys("dummyPassword");

driver.findElement(By.xpath("//button[@name='login']")).click();

5. Wikipedia.org ? Extract Title

driver.get("https://en.wikipedia.org/wiki/Selenium_(software)");

WebElement heading = driver.findElement(By.xpath("//h1[@id='firstHeading']"));

System.out.println("Page title: " + heading.getText());

6. Demo Login Page ? Form Submit

driver.get("https://the-internet.herokuapp.com/login");

driver.findElement(By.xpath("//input[@id='username']")).sendKeys("tomsmith");

driver.findElement(By.xpath("//input[@id='password']")).sendKeys("SuperSecretPassword!");

driver.findElement(By.xpath("//button[@type='submit']")).click();

7. YouTube.com ? Search Video

driver.get("https://www.youtube.com/");

driver.findElement(By.xpath("//input[@id='search']")).sendKeys("XPath Tutorial");

driver.findElement(By.xpath("//button[@id='search-icon-legacy']")).click();

Thread.sleep(2000);

driver.findElement(By.xpath("(//a[@id='video-title'])[1]")).click();

8. Wait for Dynamic Elements

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));

WebElement button = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[text()='Submit']")));

button.click();

You might also like