Introduction to Selenium
Selenium WebDriver Basics
WebDriver Commands
Synchronization in Selenium
Working with Different Browsers
Setting up WebDriver for different browsers
Handling Advanced User Interactions
Page Object Model (POM)
Introduction to POM
TestNG Framework
Creating and Running TestNG Tests
Selenium is a powerful tool for automating web browsers through programs and performing browser tasks. It is widely used for testing web applications. Below, Here isĀ  provide an example of a simple Selenium script in Java and explain the output.

Example Script

1. Setup:

Ensure you have the following dependencies in your ‘pom.xml‘ if you are using Maven:
				
					<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>4.4.0</version>
</dependency>
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-chrome-driver</artifactId>
    <version>4.4.0</version>
</dependency>

				
			

2. Java Code:

Here is a simple Selenium script that opens a browser, navigates to a webpage, and prints the page title:
				
					import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

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

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

        // Navigate to a web page
        driver.get("https://www.example.com");

        // Get the title of the page
        String title = driver.getTitle();
        System.out.println("Page title is: " + title);

        // Find an element on the page
        WebElement exampleElement = driver.findElement(By.id("exampleId"));

        // Perform actions on the element (e.g., click, send keys)
        exampleElement.click();

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

				
			

Understanding the Output

1. Launching Browser:

When you run the script, it will launch a Chrome browser window. This is done through the WebDriver interface and the ChromeDriver implementation.

2. Navigating to a Web Page:

The line ‘driver.get("https://www.example.com");‘ instructs the browser to navigate to “https://www.example.com“.

3. Printing the Page Title:

The line ‘String title = driver.getTitle();‘ retrieves the title of the webpage, which is then printed to the console with ‘System.out.println("Page title is: " + title);‘. The output in the console will be something like:
				
					Page title is: Example Domain

				
			

4. Interacting with Web Elements:

The script attempts to find an element by its ID ‘exampleId with driver.findElement(By.id("exampleId"));‘. If such an element is found, actions like click() can be performed on it. If the element is not found, an exception will be thrown.

5. Closing the Browser:

Finally, ‘driver.quit();‘ closes the browser window and ends the WebDriver session.

Notes

  • Path to chromedriver: Ensure the path to chromedriver is correctly set in System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");.
  • Exception Handling: It is good practice to include exception handling to manage scenarios where elements are not found or other errors occur.
  • WebDriver Waits: For dynamic web pages, you might need to include waits (implicit or explicit) to ensure elements are loaded before interacting with them.
Scroll to Top