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………………


15 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. Chris LaVenture September 29, 2017 / 7:00 pm

    I am sorry I am seeing the value placed in my box using the code in your second example, but when I execute the function button on that page it does not see the value I have placed that box.


    • Chris LaVenture September 29, 2017 / 9:09 pm

      Yet if I do it with Sendkeys it, of course, is really slow per character but accepts the operand. I don’t understand how I can see it in the text box with that nice little JS Executor snippet you showed us but the function/query generator button doesn’t see it


    • Chris LaVenture September 29, 2017 / 9:17 pm

      I am using this code (which compiles, shows the text going in, but is not recognized when I press my query button):

      IWebElement mySelStrucNumBox = _driver.FindElement(By.Name(“builder_rule_0_value_0”));
      IJavaScriptExecutor myExecutor = ((IJavaScriptExecutor)_driver);
      myExecutor.ExecuteScript(“arguments[0].value=’CXE-191719′;”, mySelStrucNumBox);

      To Replace this Code (which works but is slow a character at a time):

      The next thing I do is excuter the query with that right operand as the search criteria:


  3. 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.


  4. 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.


  5. 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 ?


      • Induraj February 16, 2018 / 1:07 pm

        Hello, has any one got any work around for this?


      • Kirtesh Wani February 16, 2018 / 3:08 pm

        Hey Girija, as application is in angularjs and its all about events to update the scope(data) of the application.

        When you use JavascriptExecutor to set any value, it does not triggers any event. To update the scope it mandatory to be an event because angularjs works in that way.

        Good news is you can trigger event through javascript which can fix your issue.

        use method $().trigger(‘change’);

        below is the code to set your email id field by javascriptexecutor

        WebElement txtEmailId = driver.findElement(By.xpath("//input[@id='email']")); 


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