Monday, 18 February 2013

composing an email using selenium web driver

composing an email using selenium web driver

 I tried composing an email from gmail using selenium webdriver.
I found a better way to enter text without switching frames.
Please refer to the below code.



import java.util.concurrent.TimeUnit;

//import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
//import org.openqa.selenium.WebElement;
//import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
//import org.openqa.selenium.support.ui.Select;

public class composingAnEmail {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

    WebDriver d1;
    d1=new FirefoxDriver();
    d1.get("https://mail.google.com/");
    d1.findElement(By.id("Email")).sendKeys("Your mail Id");
    d1.findElement(By.xpath("//*[@id='Passwd']")).sendKeys("Your password");
    d1.findElement(By.id("signIn")).click();
    d1.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
    d1.findElement(By.xpath("/html/body/div[5]/div[2]/div/div[2]/div/div/div/div[2]/div/div/div/div/div")).click();
    d1.findElement(By.xpath("//textarea[@name='to']")).sendKeys("receiver's Id");
     d1.findElement(By.name("subject")).sendKeys("test mail");
  //I felt this method works more better than switching frames.
    d1.findElement(By.xpath("//iframe[@class= 'Am Al editable']")).click();
    d1.findElement(By.xpath("//iframe[@class= 'Am Al editable']")).sendKeys("Hi ! How are you ?This is my first email using selenium webdriver.");
    d1.findElement(By.xpath("//div[@class='T-I J-J5-Ji Bq nS T-I-KE L3']")).click();
    d1.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
    d1.quit();
    }
}

5 comments:

  1. thank you for the code

    ReplyDelete
  2. this code is not working for the line:

    d1.findElement(By.xpath("//iframe[@class= 'Am Al editable']")).click();
    d1.findElement(By.xpath("//iframe[@class= 'Am Al editable']")).sendKeys("Hi ! How are you ?This is my first email using selenium webdriver.");

    could you plz help me for the same

    ReplyDelete
  3. Hi,

    I am automating gmail and i have tries this code, and i am able to click on compose button but not able to enter data in "to" field of compose mail..can you help me in this?

    ReplyDelete
  4. This is very fragile code.
    What is especially terrible:

    1. Don't use an absolute XPATH like "/html/body/div[5]/div[2]/div/div[2]/div/div/div/div[2]/div/div/div/div/div" Better to design something more flexible like: "//div[text()='COMPOSE']"

    2. "//div[@class='T-I J-J5-Ji Bq nS T-I-KE L3']" - Google does not use static IDs and class names.
    "//div[text()='Send']" will work better.

    ReplyDelete