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

Sending keyboard inputs in Selenium using Java can be accomplished using the sendKeys method, which is part of the WebElement interface. This method allows you to simulate typing into elements such as text fields or text areas. Additionally, you can perform keyboard actions like pressing keys, key combinations, or even complex sequences of key actions using the Actions class.

1. Basic Text Input with 'sendKeys'

To type into a text field, you simply locate the 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 SendKeysExample {
    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://example.com");

        // Locate the text field element
        WebElement textField = driver.findElement(By.id("textFieldId"));

        // Type into the text field
        textField.sendKeys("Hello, World!");

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

				
			

2. Using Actions Class for Key Presses

The Actions class provides more advanced keyboard interactions, such as pressing keys or combinations of keys.

Example: Pressing Enter Key

				
					import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.Keys;

public class ActionsExample {
    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://example.com");

        // Locate the text field element
        WebElement textField = driver.findElement(By.id("textFieldId"));

        // Create an instance of Actions class
        Actions actions = new Actions(driver);

        // Type text and press Enter
        actions.sendKeys(textField, "Hello, World!").sendKeys(Keys.ENTER).build().perform();

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

				
			

Example: Key Combinations (e.g., Ctrl+A, Ctrl+C, Ctrl+V)

				
					import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.Keys;

public class KeyCombinationExample {
    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://example.com");

        // Locate the text field element
        WebElement textField = driver.findElement(By.id("textFieldId"));

        // Type some text
        textField.sendKeys("Some text");

        // Create an instance of Actions class
        Actions actions = new Actions(driver);

        // Select all text (Ctrl+A), copy (Ctrl+C), and paste (Ctrl+V)
        actions.keyDown(Keys.CONTROL)
               .sendKeys("a")
               .sendKeys("c")
               .keyUp(Keys.CONTROL)
               .sendKeys(Keys.TAB) // Assuming the next field is navigated by tab
               .keyDown(Keys.CONTROL)
               .sendKeys("v")
               .keyUp(Keys.CONTROL)
               .build().perform();

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

				
			

3. Complex Sequences with Actions Class

For more complex sequences involving multiple actions, the ‘Actions‘ class allows chaining actions.

Example: Typing Text with Shift Modifier

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

public class ComplexActionsExample {
    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://example.com");

        // Locate the text field element
        WebElement textField = driver.findElement(By.id("textFieldId"));

        // Create an instance of Actions class
        Actions actions = new Actions(driver);

        // Type text with shift pressed to make it uppercase
        actions.click(textField)
               .keyDown(Keys.SHIFT)
               .sendKeys("hello")
               .keyUp(Keys.SHIFT)
               .build().perform();

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

				
			

Using the ‘sendKeys‘ method and the ‘Actions‘ class in Selenium WebDriver allows for a wide range of keyboard interactions. The ‘sendKeys‘ method is straightforward for simple text input, while the ‘Actions‘ class offers more flexibility for complex keyboard operations.

Scroll to Top