How to input text in the text box without calling the sendKeys() using Selenium java?

Last Updated : 23 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

When automating web applications using Selenium WebDriver, the standard approach to input text into a text box is by using the sendKeys() method. However, there are scenarios where you might want to input text without calling sendKeys(). This can be achieved using JavaScriptExecutor in Selenium. JavaScriptExecutor allows direct manipulation of the DOM, providing more flexibility in controlling elements on a web page, especially when sendKeys() might not work due to specific conditions or dynamic elements.

JavaScript Executor

JavaScript Executor allows you to run JavaScript code within the browser context. This method directly interacts with the web page's DOM, allowing you to set the value of an input field without simulating keystrokes.

Syntax

JavascriptExecutor js = (JavascriptExecutor) driver;

WebElement textBox = driver.findElement(By.className("HomePageSearchContainer_homePageSearchContainer_container_input__1LS0r"));

js.executeScript("arguments[0].value='Selenium';", textBox);

Example to input text in the text box without calling the sendKeys() using Selenium java

Java
package seleniumpractice;

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.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.Duration;
import org.openqa.selenium.JavascriptExecutor;

public class JsEnterText {
    public static void main(String[] args) {
        // Set the path for the ChromeDriver executable
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver"); // Replace with your local path
        
        // Initialize ChromeDriver
        WebDriver driver = new ChromeDriver();
        
        // URL launch
        driver.get("https://www.geeksforgeeks.org/");
        
        // Explicit wait
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
    
        // JavaScript Executor to enter text
        JavascriptExecutor js = (JavascriptExecutor) driver;
        WebElement textBox = driver.findElement(By.className("HomePageSearchContainer_homePageSearchContainer_container_input__1LS0r"));

        js.executeScript("arguments[0].value='Selenium';", textBox);
        
        // Verify value
        String value = textBox.getAttribute("value");
        System.out.println("Value entered is: " + value);
        
        // Cleanup
        driver.quit();
    }
}

Explation

1. Import statements

  • Imports statement include classes from , WebDriverManager, and Java's Duration class, which are necessary for interacting with the browser and manipulating web elements.

2. Class Declaration

public class JsEnterText

  • The class JsEnterText contains the main method where the script execution starts.

3. Main Method

  • Main method is the entry point of the program. where its executes.

4. WebDriver Setup

WebDriverManager.chromedriver().setup();

WebDriver driver = new ChromeDriver();

  • WebDriverManager automatically manages the ChromeDriver binary.
  • ChromeDriver initializes a new Chrome browser instance.

5. Navigate to URL

driver.get("https://www.geeksforgeeks.org/");

  • Opens the specified URL in the browser.

6. WebDriverWait (Explicit Wait):

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));

  • Initializes an explicit wait with a timeout of 10 seconds. However, in this particular scripts, wait is not used further. it's prepared to handle waits if needed, but in this case, you directly find the element and execute JavaScript.

7. JavaScript Executor:

JavascriptExecutor js = (JavascriptExecutor) driver;

WebElement textBox = driver.findElement(By.id("mobile-search-strings"));

WebElement textBox = driver.findElement(By.className("HomePageSearchContainer_homePageSearchContainer_container_input__1LS0r"));

js.executeScript("arguments[0].value='Selenium';", textBox);

  • Casts the driver to JavascriptExecutor, which allows running JavaScript commands.
  • Locates the text box element by its className(("HomePageSearchContainer_homePageSearchContainer_container_input__1LS0r"));
  • Uses JavaScript to set the value attribute of the text box to 'Selenium'.

8. Verify and Print Value:

String value = textBox.getAttribute("value");

System.out.println("Value entered is: " + value);

  • Retrieves the value attribute from the box and prints it to the console. This should display the text that was set via JavaScript.

Output

output
Output

Conclusion

In conclusion, while the sendKeys() method is a common approach to enter text in Selenium WebDriver, using JavaScriptExecutor offers an alternative and more flexible way of inputting text into web elements. By bypassing sendKeys(), you can directly interact with the DOM, making it easier to handle complex or dynamic web pages. applying JavaScriptExecutor ensures your automation scripts are versatile and capable of handling different web application behaviors.


Next Article
Article Tags :

Similar Reads