Selenium WebDriver
Day 10: Selenium WebDriver - Browser Operations
Introduction to Selenium WebDriver with browser operations: opening browsers, navigation, capturing URLs and titles
Today's Learning Objectives
Selenium Introduction
- • Web-based automation using Selenium
- • Browser and Web Object operations
- • Setting up Selenium Maven project
- • ChromeDriver and EdgeDriver usage
Browser Operations
- • Opening different browsers (Chrome, Edge, Safari)
- • URL navigation and page operations
- • Capturing browser title and current URL
- • Window management and session control
Introduction to Selenium WebDriver
Web-Based Automation Using Selenium
Selenium WebDriver is a powerful tool for automating web applications. It provides two main categories of operations:
1. Browser Related Operations
- • Connect to actual browser
- • Open Chrome/Edge/Opera/Safari/IE
- • Open URLs and navigate
- • Capture browser title and URL
- • Window and tab management
- • Browser navigation (back, forward, refresh)
2. Web Object Related Operations
- • Locate web elements
- • Interact with forms and inputs
- • Handle dropdowns and selections
- • Click buttons and links
- • Extract text and attributes
- • Handle dynamic content
Browser Related Operations
Opening Different Browsers
To open a browser, we create an object of the respective Driver class using the new
keyword.
Chrome Browser
// Syntax
ChromeDriver abc = new ChromeDriver();
// Complete Example
public class Demo1 {
public static void main(String[] args) {
ChromeDriver abc = new ChromeDriver();
}
}
ChromeDriver abc = new ChromeDriver();
// Complete Example
public class Demo1 {
public static void main(String[] args) {
ChromeDriver abc = new ChromeDriver();
}
}
Edge Browser
EdgeDriver xyz = new EdgeDriver();
Opening URLs
Use the get()
method to open a URL in the browser.
// Open Chrome browser and navigate to Facebook
ChromeDriver abc = new ChromeDriver();
abc.get("https://www.facebook.com");
// Open Edge browser and navigate to Google
EdgeDriver driver = new EdgeDriver();
driver.get("https://www.google.com");
ChromeDriver abc = new ChromeDriver();
abc.get("https://www.facebook.com");
// Open Edge browser and navigate to Google
EdgeDriver driver = new EdgeDriver();
driver.get("https://www.google.com");
Capturing Browser Information
Capture Browser Title
ChromeDriver abc = new ChromeDriver();
abc.get("https://www.facebook.com");
String title = abc.getTitle();
System.out.println(title); // Prints: Facebook – log in or sign up
abc.get("https://www.facebook.com");
String title = abc.getTitle();
System.out.println(title); // Prints: Facebook – log in or sign up
Capture Current URL
ChromeDriver abc = new ChromeDriver();
abc.get("https://www.facebook.com");
String currentUrl = abc.getCurrentUrl();
System.out.println(currentUrl); // Prints current URL
abc.get("https://www.facebook.com");
String currentUrl = abc.getCurrentUrl();
System.out.println(currentUrl); // Prints current URL
Navigation Operations
Navigation Methods
- navigate().to(url) - Navigate to another URL
- navigate().back() - Navigate back to previous page
- navigate().forward() - Navigate forward to next page
- navigate().refresh() - Refresh the current page
ChromeDriver abc = new ChromeDriver();
// Open Google
abc.get("https://www.google.com");
// Navigate to Facebook
abc.navigate().to("https://www.facebook.com");
// Go back to Google
abc.navigate().back();
// Go forward to Facebook
abc.navigate().forward();
// Refresh the page
abc.navigate().refresh();
// Open Google
abc.get("https://www.google.com");
// Navigate to Facebook
abc.navigate().to("https://www.facebook.com");
// Go back to Google
abc.navigate().back();
// Go forward to Facebook
abc.navigate().forward();
// Refresh the page
abc.navigate().refresh();
Window Management
Closing Windows and Sessions
- close() - Closes the focused window (does not terminate session)
- quit() - Closes all windows and terminates the session
ChromeDriver abc = new ChromeDriver();
abc.get("https://www.facebook.com");
String title = abc.getTitle();
System.out.println(title);
String url = abc.getCurrentUrl();
System.out.println(url);
// Close window and terminate session
abc.quit();
abc.get("https://www.facebook.com");
String title = abc.getTitle();
System.out.println(title);
String url = abc.getCurrentUrl();
System.out.println(url);
// Close window and terminate session
abc.quit();
Adding Delays
// Stop execution for 8 seconds
Thread.sleep(8000);
// Note: Add throws InterruptedException to main method
public static void main(String[] args) throws InterruptedException
Thread.sleep(8000);
// Note: Add throws InterruptedException to main method
public static void main(String[] args) throws InterruptedException
Selenium Project Setup
Creating Selenium Maven Project
Setup Steps:
- Open Eclipse IDE
- Click File → New → Maven Project → Next
- Select Apache POI Quick Start 1.1/1.4 version
- Enter Group Id and Artifact Id → Finish
- Open pom.xml file
- Add Selenium dependency (version 4.16.1)
- Close pom.xml file
- Create package and class
<!-- Add this dependency to pom.xml -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.16.1</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.16.1</version>
</dependency>
Day 10 Knowledge Check
Question 1 of 5
Which method is used to open a URL in Selenium WebDriver?
Practice Assignment
🎯 Complete Browser Navigation Assignment
Task: Create a comprehensive browser navigation program
- Open Edge browser
- Open Wikipedia URL
- Capture and print title
- Navigate to Facebook URL
- Capture and print current URL
- Navigate back to Wikipedia
- Capture and print current URL
- Navigate forward to Facebook
- Capture and print title
- Refresh the page
- Close window and terminate session
Key Takeaways
Browser Operations
- Opening Browsers: ChromeDriver, EdgeDriver objects
- URL Navigation: get() method for opening URLs
- Information Capture: getTitle(), getCurrentUrl()
- Navigation: navigate().to(), back(), forward(), refresh()
Project Setup
- Maven Project: Eclipse IDE with Maven structure
- Dependencies: Selenium Java 4.16.1 in pom.xml
- Package Structure: Organized class and package creation
- Import Statements: org.openqa.selenium packages
Window Management
- close(): Closes focused window only
- quit(): Closes all windows + terminates session
- Thread.sleep(): Add delays for observation
- Exception Handling: throws InterruptedException
Best Practices
- Always use quit(): Properly terminate sessions
- Meaningful variable names: Clear driver object names
- Exception handling: Handle InterruptedException
- Resource cleanup: Close browsers after testing
💡 Master these browser operations as they form the foundation for Day 11 where we'll learn about locators and web element interactions!