0
votes

I was automating Gmail to send a mail with attachment. The mail contains the in-build signature text of the sender. Every time when I want to type anything in the mail body, the text appears always after the "Regards" and name field. Below is my code.

driver.findElement(By.xpath(".//*[text()= 'COMPOSE']")).click();
wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//textarea[contains(@aria-label, 'To')]")));
driver.findElement(By.xpath(".//textarea[contains(@aria-label, 'To')]")).click();
driver.findElement(By.xpath(".//textarea[contains(@aria-label, 'To')]")).sendKeys("[email protected]");
driver.findElement(By.name("subjectbox")).click();
driver.findElement(By.name("subjectbox")).sendKeys("efgh");
driver.findElement(By.xpath("(.//*[@aria-label='Message Body'])[2]")).click();
driver.findElement(By.xpath("(.//*[@aria-label='Message Body'])[2]")).sendKeys("This is an auto-generated mail");

One solution I am using is as below:

driver.findElement(By.xpath("(.//*[@aria-label='Message Body'])[2]")).click();
String s = "Sir,\n This is a auto generated email. \n\n" 
    + driver.findElement(By.xpath("(.//*[@aria-label='Message Body'])[2]")).getText();
driver.findElement(By.xpath("(.//*[@aria-label='Message Body'])[2]")).clear();
driver.findElement(By.xpath("(.//*[@aria-label='Message Body'])[2]")).sendKeys(s);

instead of these two line: driver.findElement(By.xpath("(.//[@aria-label='Message Body'])[2]")).click(); driver.findElement(By.xpath("(.//[@aria-label='Message Body'])[2]")).sendKeys("This is an auto-generated mail");

But this solution actually removes all the format of that signature text. Even with signature text, I am not able to get the xpath of the text box using Firebug, I have to delete the complete signature text to get those. I am a beginner in this automation field. Please help how can I write the text in the mail body before the signature text.

4
plz do it like form code you enter signature ,suitable and accurate way - eduliant
But why? You can simply send emails via SMTP if you want to do it programatically... This has got to be the most Rube Goldberg solution ever. - max
@rajNishKuMar, sorry I am not getting you properly. Actually, manually we can type the text in body by clicking on the text area. The signature appears just below the text. But, using selenium, how can I perform this? - Samiran Banerjee
@max, can I use it through selenium webdriver? - Samiran Banerjee
No - SMTP is a protocol used for example to create desktop email clients - you don't need to automate a web browser at all to send email programically, you just set the content, and headers and off it goes. Automating a browser to use the gmail web is actually the most impractical and convoluted solution ever since it will break if the Gmail team changes the front-end. - max

4 Answers

1
votes

@Samiran Banerjee

just use the below code:

driver.findElement(By.cssSelector(".Am.Al.editable.LW-avf>br")).click();
driver.findElement(By.cssSelector(".Am.Al.editable.LW-avf")).sendKeys("This is an auto-generated mail");

this will type before your signature.

But enter "To" and "subject" and than, wait one or two second.

1
votes

Hi plz try like below

public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub
        System.setProperty("webdriver.chrome.driver", "D:\\eclipseProject\\StackOverFlow\\chromedriver_win32 (1)\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
        driver.get("https://accounts.google.com/ServiceLogin?");
        // gmail login
        driver.findElement(By.id("Email")).sendKeys("XXXXXXXXXXX.com");
        driver.findElement(By.id("next")).click();
        Thread.sleep(1000);
        driver.findElement(By.id("Passwd")).sendKeys("XXXXXXXXXX");
        driver.findElement(By.id("signIn")).click();

        // some optional actions for reaching gmail inbox
        driver.findElement(By.xpath("//*[@title='Google apps']")).click();
        driver.findElement(By.id("gb23")).click();
        // clicks compose
        driver.findElement(By.cssSelector(".T-I.J-J5-Ji.T-I-KE.L3")).click();
        // types message in body without hampering signature
        driver.findElement(By.id(":pg")).sendKeys("This is an auto-generated mail");;

    }

Hope this solves your problem look image for better understanding

enter image description here

also see id in the source code

enter image description here

1
votes
public static void main(String[] args) throws InterruptedException {
    // TODO Auto-generated method stub
    System.setProperty("webdriver.chrome.driver", "D:\\eclipseProject\\StackOverFlow\\chromedriver_win32 (1)\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
    driver.get("https://accounts.google.com/ServiceLogin?");
    // gmail login
    driver.findElement(By.id("Email")).sendKeys("XXXXXXXXXXX.com");
    driver.findElement(By.id("next")).click();
    Thread.sleep(1000);
    driver.findElement(By.id("Passwd")).sendKeys("XXXXXXXXXX");
    driver.findElement(By.id("signIn")).click();

    // some optional actions for reaching gmail inbox
    driver.findElement(By.xpath("//*[@title='Google apps']")).click();
    driver.findElement(By.id("gb23")).click();
    // clicks compose
    driver.findElement(By.cssSelector(".T-I.J-J5-Ji.T-I-KE.L3")).click();
    // types message in body without hampering signature
    driver.findElement(By.id(":pg")).sendKeys("This is an auto-generated mail");;

}
1
votes
class Gmail_1 {

    public static void main(String[] args) throws InterruptedException {

    System.setProperty("webdriver.firefox.marionette","C:\\geckodriver.exe");
    WebDriver driver = new FirefoxDriver();    
    driver.manage().window().maximize();
    String url = "https://accounts.google.com/signin";
    driver.get(url);
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 

    WebElement email_phone = driver.findElement(By.xpath("//input[@id='identifierId']"));
    email_phone.sendKeys("*******.com");
    driver.findElement(By.id("identifierNext")).click();
    WebElement password = driver.findElement(By.xpath("//input[@name='password']"));
    WebDriverWait wait = new WebDriverWait(driver, 20);
    wait.until(ExpectedConditions.elementToBeClickable(password));
    System.out.println("\ntest  password");
    password.sendKeys("a*******3");                     
    driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS); 
    password.sendKeys(Keys.ENTER);
//    driver.findElement(By.className("ZFr60d")).click();
    driver.findElement(By.className("WaidBe")).click();
    System.out.println("Mail page opened");
    Thread.sleep(3000);
    driver.findElement(By.className("z0")).click();
    driver.findElement(By.className("vO")).sendKeys("****************.com");
    driver.findElement(By.className("aoT")).sendKeys("Test email from selenium");
    driver.findElement(By.className("Am")).sendKeys("Hi");
    driver.findElement(By.className("aoO")).click();



    }
}