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