example on how to automate google spread sheet usi

2019-03-31 13:22发布

问题:

This one is just a knowledge share example. This is my example for automation for google spread sheet via gmail using selenium webdriver

 package com.google;

    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.interactions.Actions;
    import org.openqa.selenium.Alert;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.By;


    public class testgoogle {

        static WebDriver driver;
        static WebElement element;

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

            driver = new FirefoxDriver();
            driver.get("http://www.gmail.com");
            //sign in
            driver.findElement(By.linkText("Sign in")).click();
            driver.findElement(By.id("Email")).sendKeys("username");
            driver.findElement(By.id("Passwd")).sendKeys("password");
            driver.findElement(By.id("signIn")).click();
            //sleep till page is loaded --necessary
            Thread.sleep(15000);


            String parentHandle = driver.getWindowHandle(); 

            driver.findElement(By.xpath(".//*[@id='gbwa']/div[1]/a")).click();
            driver.findElement(By.id("gb25")).click();
            //Perform the click operation that opens new window
            System.out.println("this one should be the gmail" + driver.getCurrentUrl());

                     // switching the window to google drive
            for (String winHandle : driver.getWindowHandles()) {
                driver.switchTo().window(winHandle); 

            }

            System.out.println("this one should be the google drive" + driver.getCurrentUrl());
            element = driver.findElement(By.xpath(".//*[@id='navpane']/div[2]/div[1]/div/div/div[1]"));
            new Actions(driver).moveToElement(element).click().perform();

            driver.findElement(By.xpath(".//*[@id=':1t.cmi']")).click();

                    // switching it to google sheet window from google drive window
            String parent2Handle = driver.getWindowHandle(); 
            for (String winHandle : driver.getWindowHandles()) {
                driver.switchTo().window(winHandle);
                Thread.sleep(1000);
            }

            System.out.println("this one should be the spread sheet" + driver.getCurrentUrl());
            Thread.sleep(5000);
            driver.findElement(By.xpath("//table/tbody/tr/td[3]/div/div/div")).sendKeys("hello this is first field");

            driver.close();
                     // accept the leave window alert.
            Alert alert = driver.switchTo().alert();
            alert.accept();
                     //closes the google drive window
            driver.switchTo().window(parent2Handle);
            driver.close();
                    // closes the gmail window
            driver.switchTo().window(parentHandle);
            driver.close();

        }
    }