1

I am trying to send email using Selenium , but I failed to locate the To field and email body.

This my code:

@Test
public  void createEmail(){

      WebDriver driver= new FirefoxDriver();
      driver.get("http://gmail.com/");

      driver.findElement(By.name("Email")).sendKeys("myUsername");;
      driver.findElement(By.name("Passwd")).sendKeys("myPassword");;
      driver.findElement(By.name("signIn")).click();

      driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);

      // create the Email
      driver.findElement(By.cssSelector("span.gb_7.gbii")).click();
      driver.findElement(By.id("gb_71")).click();   
      driver.findElement(By.name("to")).sendKeys("test@gmail.com");
}
  • 1
    Which exception are you getting? – FDM Feb 14 '15 at 20:30
  • It's quite difficult to send email in gmail browser version using selenium. However you can try "Basic html" version of Gmail to send mails. – Hideandseek Feb 15 '15 at 12:49
  • 2
    Are those your real username and password in the script? – Bigwave Feb 15 '15 at 19:52
  • When you have more reputation, you'll be able to put comments like this where they belong: as comments on question. You could make this a real answer by editing and describing how you'd use GMail's basic HTML mode to send mail. – Kate Paulk Feb 16 '15 at 12:16
  • What is your goal? Do you want to send email though Gmail or you want to use Selenium? – amazpyel Feb 17 '15 at 8:45
0

You could try this, I found in another post.

driver.findElement(By.id("to")).clear();
driver.findElement(By.id("to").sendKeys("data");

The TO field is a textarea. Adding .clear() seems to set the focus correctly, maybe there are other ways to give the textarea focus first.

https://stackoverflow.com/questions/11970240/how-to-change-text-in-textarea-with-webdriver

Not the answer you're looking for? Browse other questions tagged or ask your own question.