XPath in Selenium WebDriver 🚀
By Bhau Automation • XPath Tutorial for Beginners
🎯 What You Will Learn
- What is XPath in Selenium?
- XPath syntax and structure
- How to create XPath expressions
- XPath examples using attributes
- Finding elements in Chrome browser
💡 XPath is one of the most powerful locators used in Selenium automation testing.
📌 What is XPath?
XPath stands for XML Path. It is a syntax or language used to locate and identify web elements on a webpage using path expressions.
XPath helps Selenium identify elements when ID, Name, or other locators are not available.
🛠️ XPath Syntax
Basic syntax to create XPath:
//tagname[@attribute="value"]
🔍 XPath Examples
1. XPath for Password Field
//input[@id="pass"]
2. XPath for Login Button
//input[@value="Log In"]
3. XPath using Any Tag
//*[@id="u_0_4"]
🌐 XPath in Chrome Browser
You can inspect webpage elements in Chrome browser using:
- Right Click → Inspect
- Use Chrome Developer Tools
- Copy XPath directly from elements
💻 Selenium XPath Example Program
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class XPathDemo {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.get("https://www.facebook.com");
driver.findElement(By.xpath("//input[@id="email"]"))
.sendKeys("test@gmail.com");
driver.findElement(By.xpath("//input[@id="pass"]"))
.sendKeys("password");
driver.findElement(By.xpath("//input[@value="Log In"]"))
.click();
}
}
⚡ Types of XPath
- Absolute XPath
- Relative XPath
Relative XPath is mostly preferred because it is flexible and easy to maintain.
🌍 Real-World Use Cases
- Locating dynamic elements
- Handling complex webpages
- Automation framework development
- Cross-browser automation testing
🎥 Watch Complete Video Tutorial
👉 Watch XPath Tutorial in Selenium
🎓 Key Takeaways
- XPath is widely used in Selenium automation
- XPath helps locate web elements effectively
- Relative XPath is preferred over absolute XPath
- XPath knowledge is essential for automation testers
- Very important Selenium interview topic
⚡ Pro Tip: Practice creating XPath expressions daily using different websites to improve your Selenium automation skills.
🚀 Created with ❤️ by Bhau Automation