Enter value in textfield using Javascript

We can enter text into text field/ text area using the most popular method sendKeys() of WebDriver API. But, there is an alternate way to achieve the same using Selenium WebDriver.

We can achieve the same using JavascriptExecutor interface provided by WebDriver API.

Lets have a look at the working demo for the same.

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class SendKeysUsingJavacsript {

             public static void main(String[] args) {
                          WebDriver driver = new FirefoxDriver();
                          driver.get(“http://google.com”);
                          driver.manage().window().maximize();

                           JavascriptExecutor js = (JavascriptExecutor) driver;
                           js.executeScript(“document.getElementById(‘lst-                  ib’).value=’selenium'”);

                }
}

 

Advertisements

#javascriptexecutor