Selenium WebDriver Commands: Automating Web Browser Actions and Interactions
Master essential Selenium WebDriver commands for automating web browser interactions. This comprehensive guide categorizes common commands (locating elements, interacting with forms, navigating pages), provides examples using Java, and helps you efficiently automate web browser tasks.
Selenium WebDriver Commands: Automating Web Browser Actions
Selenium WebDriver provides a set of commands (methods) for automating web browser interactions. These commands allow you to simulate user actions like clicking buttons, typing text, selecting options, navigating pages, and more. This guide categorizes common WebDriver commands and provides examples. The examples below use Java.
Categories of WebDriver Commands
- Browser Commands: Manage the browser window (e.g., maximizing, closing).
- Navigation Commands: Control navigation (e.g., opening URLs, going back/forward in history).
- Web Element Commands: Interact with web page elements (e.g., typing, clicking, getting text).
Common WebDriver Commands
Command Category | Command | Description | Example |
---|---|---|---|
Navigation | driver.navigate().to("url") |
Opens a URL. | driver.navigate().to("https://www.example.com"); |
Navigation | driver.navigate().back() |
Navigates back in history. | driver.navigate().back(); |
Navigation | driver.navigate().forward() |
Navigates forward in history. | driver.navigate().forward(); |
Navigation | driver.navigate().refresh() |
Refreshes the current page. | driver.navigate().refresh(); |
Browser | driver.manage().window().maximize() |
Maximizes the browser window. | driver.manage().window().maximize(); |
Browser | driver.close() |
Closes the current browser window. | driver.close(); |
Browser | driver.quit() |
Quits the driver, closing all browser windows. | driver.quit(); |
Web Element | driver.findElement(locator).sendKeys("text") |
Types text into an element. | driver.findElement(By.id("myTextbox")).sendKeys("This is a test."); |
Web Element | driver.findElement(locator).click() |
Clicks an element. | driver.findElement(By.id("myButton")).click(); |
Web Element | driver.findElement(locator).clear() |
Clears the text from a text field. | driver.findElement(By.id("myTextbox")).clear(); |
Web Element | driver.findElement(locator).getText() |
Gets the text of an element. | String text = driver.findElement(By.id("myLabel")).getText(); |
Web Element (Dropdown) | Select select = new Select(element); select.selectByVisibleText("optionText"); |
Selects an option in a dropdown. | Select dropdown = new Select(driver.findElement(By.id("myDropdown"))); dropdown.selectByVisibleText("Option 2"); |
Window Management | driver.switchTo().window("windowHandle") |
Switches to a specific window. | driver.switchTo().window("myWindow"); |
Frame Management | driver.switchTo().frame("frameName") |
Switches to an iframe. | driver.switchTo().frame("myIframe"); |
Actions | Actions actions = new Actions(driver); actions.dragAndDrop(source, target).perform(); |
Performs drag-and-drop. | (Requires appropriate locators for `source` and `target` elements) |
Example Test Script (Java)
This script uses Firefox to open a dummy test page (https://www.testandquiz.com/selenium/testing.html), interacts with various elements, and then closes the browser. Remember to set the correct path to your `geckodriver.exe`.
Selenium Test Script (Java)
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.Select;
public class WebDriverCommands {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "path/to/geckodriver.exe");
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability("marionette", true);
WebDriver driver = new FirefoxDriver(capabilities);
driver.navigate().to("https://www.testandquiz.com/selenium/testing.html");
String text = driver.findElement(By.className("col-md-12")).getText();
System.out.println(text);
driver.findElement(By.linkText("This is a link")).click();
driver.findElement(By.id("fname")).sendKeys("Test User");
driver.findElement(By.id("fname")).clear();
driver.findElement(By.id("idOfButton")).click();
driver.findElement(By.id("male")).click();
driver.findElement(By.cssSelector("input.Automation")).click();
Select dropdown = new Select(driver.findElement(By.id("testingDropdown")));
dropdown.selectByVisibleText("Automation Testing");
driver.close();
}
}