Alternate Way for sendKeys

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

Solution!!!

The best alternative I found is JavaScript

Way 1 to Execute Script

        WebDriver driver = new FirefoxDriver();
        driver.get("http://www.google.com");
        JavascriptExecutor myExecutor = ((JavascriptExecutor) driver);
        myExecutor.executeScript("document.getElementsByName('q')[0].value='Kirtesh'");
        driver.quit();

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();
        driver.get("http://www.google.com");
        WebElement searchbox = driver.findElement(By.xpath("//input[@name='q']"));
        JavascriptExecutor myExecutor = ((JavascriptExecutor) driver);
        myExecutor.executeScript("arguments[0].value='Kirtesh';", searchbox);
        driver.quit();

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

Advertisements

16 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?

    Like

    • 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!!!

      Like

  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.

    Like

    • 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

      Like

    • 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):
      _driver.FindElement(By.Name(“builder_rule_0_value_0”)).SendKeys(“CXE-191719”);

      The next thing I do is excuter the query with that right operand as the search criteria:
      _driver.FindElement(By.CssSelector(“button.btn.btn-primary.executeQuery”)).Click();

      Like

  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.

    Like

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

      Like

      • 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.
        http://oditek.in/ikomplianzWeb/client/

        Like

  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);

    driver.get(“http://oditek.in/ikomplianzWeb/client/”);
    WebElement name = wait.until(ExpectedConditions.presenceOfElementLocated(By.name(“email”)));
    name.sendKeys(“Madhu”);

    WebElement password = wait.until(ExpectedConditions.presenceOfElementLocated(By.name(“pass”)));
    password.sendKeys(“Welcome@123”);

    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();
    Thread.sleep(3000);
    driver.quit();

    Like

    • 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(By.id(“email”));
      WebElement passwordElement = driver.findElement(By.id(“pass”));
      executor.executeScript(“arguments[0].value=’tester'”, emailElement);
      executor.executeScript(“arguments[0].value=’testpass'”, passwordElement);
      driver.findElement(By.xpath(“//form[@id=’loginForm’]/button”)).click();

      Like

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

        Like

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

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

    Like

    • Girija October 4, 2017 / 7:05 pm

      Hello, did you get any fix or alternatives ?

      Like

      • Induraj February 16, 2018 / 1:07 pm

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

        Like

      • Kirtesh March 16, 2018 / 12:09 pm

        Hey, 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']")); 
        ((JavascriptExecutor)driver).executeScript("arguments[0].value='email@domain.com';$(arguments[0]).trigger('change');",txtEmailId);
        

        Like

      • 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']")); 
        
        ((JavascriptExecutor)driver).executeScript("arguments[0].value='email@domain.com';$(arguments[0]).trigger('change');",txtEmailId);
        

        Liked by 1 person

Leave a Reply

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

WordPress.com Logo

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

Google+ photo

You are commenting using your Google+ 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 )

w

Connecting to %s