Friday, 24 April 2015

Alternative to sendkeys()

SendKeys() not working? Not to worry...

My computer got crashed and on the prosses of downloading  selenium and eclipse ,I think I messed up with downloading correct version of java compiler or for the reasons I do not know....somehow sending text using sendKeys() seemed to be an impossible task...

Here I came up with an  alternative for it using JavascriptExecutor.
**Thanks to Kritesh **
Simple example for the substitution of sendKeys using Gmail login..



import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;


public class gmail_login {

    /**

     * @param <string>
     * @param args
     * @throws InterruptedException
     */
    public static <sendKeys, string> void main(String[] args) throws InterruptedException
    {
       
      
        WebDriver driver = new FirefoxDriver();
        driver.get("http://gmail.com");
        JavascriptExecutor myExecutor = ((JavascriptExecutor) driver);
        WebElement mailid = driver.findElement(By.xpath("//input[@name='Email']"));
        myExecutor.executeScript("arguments[0].value='abcd@gmail.com';", mailid);
        WebElement password=driver.findElement(By.id("Passwd"));
        myExecutor.executeScript("arguments[0].value='efgh';", password);
        WebElement signin=driver.findElement(By.xpath("//input[@type='submit']"));
        signin.click();
        Thread.sleep(10000);
        driver.quit();
    }

}

4 comments:

  1. how can i open particular mail

    ReplyDelete
  2. Ajith - Open a particular unread email

    https://stackoverflow.com/questions/36759528/open-mails-from-gmail-inbox-using-selenium-webdriver-using-java

    ReplyDelete
  3. HI Shilpa Mynampati,

    Clicking a particular email where that email contains more than one like loop. EX: Forget password(14) as heading. I need to open this email and go to latest email and click on button.

    Help me to get out of this.

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

    ReplyDelete