Selenium WebDriver Tutorial: Getting Started with Java and Python

Introduction: Why Selenium WebDriver?

Selenium WebDriver is the most popular open-source tool for automating web browsers, used by QA engineers and developers to test web applications across Chrome, Firefox, and Safari. With support for Java and Python, it’s versatile for both beginners and experts.

In this tutorial, you’ll learn:

  • How to set up Selenium WebDriver with Java and Python.
  • Write your first test script (with examples).
  • Best practices for reliable automation.

Prerequisites

  1. For Java:
  2. For Python:
  3. Common:
    • ChromeDriver (matching your Chrome version).
    • Basic understanding of HTML/CSS.

Step 1: Set Up Selenium WebDriver

Java Setup

  1. Create a Maven project in Eclipse/IntelliJ.
  2. Add the Selenium dependency to pom.xml:
<dependency>  
    <groupId>org.seleniumhq.selenium</groupId>  
    <artifactId>selenium-java</artifactId>  
    <version>4.15.0</version>  
</dependency>  

Run HTML

Python Setup

  1. Install Selenium via pip:
pip install selenium  
  1. Install the ChromeDriver and add its path to your system’s PATH variable.

Step 2: Write Your First Test Script

Java Example (Test Google Search)

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

public class FirstTest {  
    public static void main(String[] args) {  
        // Set ChromeDriver path  
        System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");  

        // Initialize WebDriver  
        WebDriver driver = new ChromeDriver();  

        // Navigate to Google  
        driver.get("https://www.google.com");  

        // Print title  
        System.out.println("Page Title: " + driver.getTitle());  

        // Close browser  
        driver.quit();  
    }  
}  

Python Example (Test Google Search)

from selenium import webdriver  
from selenium.webdriver.chrome.service import Service  

# Set ChromeDriver path  
service = Service(executable_path="/path/to/chromedriver")  
driver = webdriver.Chrome(service=service)  

# Navigate to Google  
driver.get("https://www.google.com")  

# Print title  
print("Page Title:", driver.title)  

# Close browser  
driver.quit()  

Step 3: Locate Web Elements

Use Selenium’s locators (ID, XPath, CSS Selector) to interact with elements.

Java: Find Search Box and Enter Text

import org.openqa.selenium.By;  
// ...  
driver.findElement(By.name("q")).sendKeys("Selenium WebDriver");  

Python: Find Search Box and Enter Text

search_box = driver.find_element(By.NAME, "q")  
search_box.send_keys("Selenium WebDriver")  

Step 4: Add Assertions

Validate results using TestNG (Java) or pytest (Python).

Java (TestNG)

import org.testng.Assert;  
// ...  
String title = driver.getTitle();  
Assert.assertEquals(title, "Google");  

Python (pytest)

def test_title():  
    assert driver.title == "Google"  

Best Practices

  1. Use Explicit Waits: Avoid Thread.sleep(); use WebDriverWait for dynamic elements.
  2. Avoid XPath When Possible: Prefer CSS selectors for better performance.
  3. Headless Testing: Run tests in the background for speed:javaCopyChromeOptions options = new ChromeOptions(); options.addArguments(“–headless=new”); WebDriver driver = new ChromeDriver(options);
  4. Page Object Model (POM): Organize code by separating element locators and actions.

Troubleshooting Common Issues

  • Driver Path Errors: Ensure the ChromeDriver path is correct and added to PATH.
  • Element Not Found: Use explicit waits or check if the element is in an iframe.
  • Browser Compatibility: Always match ChromeDriver and Chrome versions.

Next Steps

  1. Explore Advanced Topics:
    • Data-driven testing with Excel/CSV.
    • Cross-browser testing (Firefox, Edge).
    • Integrate with CI/CD tools like Jenkins.
  2. Join CommunitiesSelenium Subreddit or Stack Overflow.

Conclusion

You’ve now written your first Selenium WebDriver script in Java and Python! With practice, you can automate complex workflows, integrate with DevOps pipelines, and become a testing pro.

Scroll to Top