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

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?

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

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

    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

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

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 )

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