com.mycompany.webcrawler.GmailAutomation.java Source code

Java tutorial

Introduction

Here is the source code for com.mycompany.webcrawler.GmailAutomation.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycompany.webcrawler;

import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

/**
 *
 * @author Brett
 */
public class GmailAutomation {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
        DesiredCapabilities dcaps = new DesiredCapabilities();

        dcaps.setCapability("takeScreenshot", true);
        //WebDriver driver = new PhantomJSDriver(dcaps);

        //Comment for PhantomJS
        ChromeDriver driver = new ChromeDriver();

        //Comment for PhantomJS
        //driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);

        driver.get(
                "https://accounts.google.com/ServiceLogin?sacu=1&scc=1&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&osid=1&service=mail&ss=1&ltmpl=default&rm=false#identifier");

        try {
            Thread.sleep(2000);

            driver.findElement(By.id("Email")).sendKeys("Your gmail goes here");

            Thread.sleep(2000);
            driver.findElement(By.id("next")).click();

            Thread.sleep(2000);
            driver.findElement(By.id("Passwd")).sendKeys("Your password goes here");

            Thread.sleep(2000);
            driver.findElement(By.id("signIn")).click();

            Thread.sleep(2000);
            driver.findElement(By.xpath("//div[@class='T-I J-J5-Ji T-I-KE L3']")).click();

            Thread.sleep(2000);
            driver.findElement(By.className("vO")).sendKeys("Who you want to send the email to");

            Thread.sleep(2000);
            driver.findElement(By.className("aoT")).sendKeys("The subject");

            Thread.sleep(2000);
            driver.switchTo().activeElement().sendKeys(Keys.TAB);
            driver.switchTo().activeElement().sendKeys("The body");

            //Comment for PhantomJS
            //driver.findElement(By.xpath("//div[@class='Am Al editable LW-avf']")).sendKeys("Test Email");

            Thread.sleep(2000);
            File file = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
            FileUtils.copyFile(file, new File("screenshot.jpg"));

            Thread.sleep(2000);
            driver.findElement(By.xpath("//div[@class='T-I J-J5-Ji aoO T-I-atl L3']")).click();

            //Comment for PhantomJS
            //Thread.sleep(2000);
            //driver.get("https://mail.google.com/mail/u/0/#sent");
        } catch (IOException | InterruptedException | WebDriverException e) {
            System.out.println(e.getMessage());
        }
    }
}