Samiran Banerjee
Samiran Banerjee

Reputation: 23

Automating Gmail to send mail using java and Selenium WebDriver with added signature text

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.

Upvotes: 0

Views: 22334

Answers (4)

Jerry George
Jerry George

Reputation: 11

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



    }
}

Upvotes: 1

user8091139
user8091139

Reputation: 11

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");;

}

Upvotes: 1

noor
noor

Reputation: 3004

@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.

Upvotes: 1

Rajnish Kumar
Rajnish Kumar

Reputation: 2938

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

Upvotes: 1

Related Questions