Alternate Way for sendKeys

One of my friend called me yesterday, he had an interview on Selenium WebDriver and got confused on 1 Question.

What is an alternative for sendKeys Method in WebDriver?

I also got stammered on this and thought in mind

Why the hell do we need this?

There are below possible reason that I could find, When we need any alternative to sendKeys.

  1. When the Text element is disabled or locked, sendKeys can not set the value in text field.
    (in my opinion, this is not the correct way of Automation, because the element is locked or disabled by intend to not allow any text insertion)
  2. When we want to write huge text as input… that time the way WebDriver work, by sending Series of characters from String one by one, and Which is Very time consuming. so to minimize that time we can use this alternate method

So now we have above two problems with sendKeys ……


The best alternative I found is JavaScript

Way 1 to Execute Script

        WebDriver driver = new FirefoxDriver();
        JavascriptExecutor myExecutor = ((JavascriptExecutor) driver);

in above example we have used javaScript to locate the Search Textbox and set the “Kirtesh” value in it.

lets look at another approach.

Way 2

        WebDriver driver = new FirefoxDriver();
        WebElement searchbox = driver.findElement(By.xpath("//input[@name='q']"));
        JavascriptExecutor myExecutor = ((JavascriptExecutor) driver);
        myExecutor.executeScript("arguments[0].value='Kirtesh';", searchbox);

second example is very controlled approach, Here we are passing WebElement as a argument to the javaScript

There is still many more remained from JavascriptExecutor ……

Keep Exploring…

Keep Automating………………


10 thoughts on “Alternate Way for sendKeys

  1. Madhu June 6, 2017 / 10:24 am

    in the first example , 2nd argument ‘searchbox’.. we need to create it as a webelement or a string?


    • Kirtesh June 6, 2017 / 10:44 am

      Sorry but by mistake ‘searchbox’ kept in the method, it is not required at all in the first example, as we are getting the object in javascript only, I have corrected the code. Thank you!!!


  2. Girija October 3, 2017 / 11:02 am

    I am trying to automate a login form by selenium webdriver (javascriptexucutor) which is built in angularjs. The script is entering data in email textbox; but when submit button is clicked error message shows that textbox is not filled. I have also used events like onkeyup, blur; but error shows these functions are not accepted.


    • Madhu Velpuri October 3, 2017 / 11:27 am

      1. First write an explicitwait upto the presence of Username element.
      2. Then clear the text field and enter data.
      3. Before clicking on Submit, get the value of user name input field and make sure it returns the value which you entered.
      4. If returns null, try to set focus on each element before doing an action on it.


      • Girija October 3, 2017 / 12:01 pm

        Sir, I have tried; its not returning the entered value. Also used focus(), still same issue. Can you please look into it. Thankful to you.


  3. Madhu Velpuri October 3, 2017 / 12:27 pm

    Hi Girija,

    Try below. I am seeing the alert saying that “Invalid Login Details”. i.e. Username and Password are being taken by the page. And also able to get the entered values.

    driver = new ChromeDriver();
    WebDriverWait wait = new WebDriverWait(driver, 10);

    WebElement name = wait.until(ExpectedConditions.presenceOfElementLocated(“email”)));

    WebElement password = wait.until(ExpectedConditions.presenceOfElementLocated(“pass”)));

    System.out.println(“Username is : ” + name.getAttribute(“value”));
    System.out.println(“Password is : ” + password.getAttribute(“value”));

    driver.findElement(By.xpath(“//button[@class=’btn btn-block’]”)).click();


    • Girija October 3, 2017 / 12:37 pm

      In case of sendKeys(); its working. But when I am trying to automate using javascriptexecutor; its not locating the textbox values. I have used onkeyup, focus, blur etc events also. I think the issue occurs due to angularJS form.
      JavascriptExecutor executor = (JavascriptExecutor) driver;
      WebElement emailElement = driver.findElement(“email”));
      WebElement passwordElement = driver.findElement(“pass”));
      executor.executeScript(“arguments[0].value=’tester'”, emailElement);
      executor.executeScript(“arguments[0].value=’testpass'”, passwordElement);


      • Madhu Velpuri October 3, 2017 / 12:49 pm

        Got it!!!

        I’ve tried with JS and able to replicate the issue. May be the issue is with angularJS form. Even though i am able to return the entered values, it still says “Enter Email Id”.

        Thanks for a nice problem. Will let you know once i know the solution.


  4. Girija October 3, 2017 / 5:58 pm

    Due to angularJS; I used Protractor framework. Still I am facing the same issue.


    • Girija October 4, 2017 / 7:05 pm

      Hello, did you get any fix or alternatives ?


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s