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, and using it with Java allows you to perform a variety of actions on web elements. Below, Here is describe how to perform some common actions using Selenium in Java, with examples for each.

Launching a Browser

First, set up your WebDriver to open a browser.

				
					import org.openqa.selenium.WebDriver;
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 WebDriver
        WebDriver driver = new ChromeDriver();

        // Open a webpage
        driver.get("https://www.example.com");

        // Perform actions...

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

				
			

Clicking Elements

To click an element, first locate it and then perform the click action.

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

public class ClickExample {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.example.com");

        // Locate the element to click
        WebElement elementToClick = driver.findElement(By.id("exampleButtonId"));

        // Perform the click action
        elementToClick.click();

        driver.quit();
    }
}

				
			

Typing Text into Input Fields

To type text into an input field, locate the input element and use the ‘sendKeys‘ method.

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

public class TypeExample {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.example.com");

        // Locate the input field
        WebElement inputField = driver.findElement(By.id("exampleInputFieldId"));

        // Type text into the input field
        inputField.sendKeys("Hello, Selenium!");

        driver.quit();
    }
}

				
			

Clearing Text from Input Fields

To clear text from an input field, locate the input element and use the ‘clear‘ method.

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

public class ClearExample {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.example.com");

        // Locate the input field
        WebElement inputField = driver.findElement(By.id("exampleInputFieldId"));

        // Clear text from the input field
        inputField.clear();

        driver.quit();
    }
}

				
			

Combining Actions

You can combine these actions to perform more complex interactions. For example, entering text into a field and then clicking a button:

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

public class CombinedExample {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.example.com");

        // Locate and clear the input field
        WebElement inputField = driver.findElement(By.id("exampleInputFieldId"));
        inputField.clear();

        // Type text into the input field
        inputField.sendKeys("Hello, Selenium!");

        // Locate and click the submit button
        WebElement submitButton = driver.findElement(By.id("exampleSubmitButtonId"));
        submitButton.click();

        driver.quit();
    }
}

				
			

Tips for Locating Elements

There are various ways to locate elements in Selenium, including:

  • By.id("elementId")
  • By.name("elementName")
  • By.className("elementClass")
  • By.tagName("elementTag")
  • By.cssSelector("cssSelector")
  • By.xpath("xpathExpression")

Choose the method that best suits the structure of the webpage you’re automating.

These examples provide a basic introduction to performing common actions in Selenium with Java. For more complex interactions, consider exploring Selenium’s advanced features like Actions class for complex gestures, WebDriverWait for handling waits, and JavaScriptExecutor for executing JavaScript directly.

Scroll to Top