example on how to automate google spread sheet using selenium webdriver [closed]

老子叫甜甜 提交于 2019-12-21 05:40:32

问题


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

        }
    }

来源:https://stackoverflow.com/questions/20325619/example-on-how-to-automate-google-spread-sheet-using-selenium-webdriver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!