使用 java 和 Selenium WebDriver 自动化 Gmail 发送邮件并添加签名文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36686613/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 01:45:20  来源:igfitidea点击:

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

javaselenium-webdriver

提问by Samiran Banerjee

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.

我正在自动化 Gmail 发送带附件的邮件。邮件包含发件人的内置签名文本。每次当我想在邮件正文中输入任何内容时,文本总是出现在“问候”和姓名字段之后。下面是我的代码。

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

而不是这两行: driver.findElement(By.xpath("(.// [@aria-label='Message Body'])[2]")).click(); driver.findElement(By.xpath("(.//[@aria-label='Message Body'])[2]")).sendKeys("这是一封自动生成的邮件");

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.

但是这个解决方案实际上删除了该签名文本的所有格式。即使使用签名文本,我也无法使用 Firebug 获取文本框的 xpath,我必须删除完整的签名文本才能获取这些文本。我是这个自动化领域的初学者。请帮助我如何在签名文本之前在邮件正文中写入文本。

回答by eduliant

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

另请参阅源代码中的 id

enter image description here

在此处输入图片说明

回答by noor

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

但是输入“收件人”和“主题”,然后等待一两秒钟。

回答by user8091139

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

}

回答by Jerry George

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



    }
}