关于如何使用SeleniumWebDriver自动化google电子表格的示例
这只是一个知识共享的例子。 这是我使用SeleniumWebDriver通过gmail实现谷歌电子数据表自动化的示例关于如何使用SeleniumWebDriver自动化google电子表格的示例,selenium,automation,webdriver,selenium-webdriver,Selenium,Automation,Webdriver,Selenium Webdriver,这只是一个知识共享的例子。 这是我使用SeleniumWebDriver通过gmail实现谷歌电子数据表自动化的示例 package com.google; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.interactions.Actions; import org.openqa.selenium.Alert; import org.openqa.se
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();
}
}
那么?你到底有什么问题?我看到很多关于这个特定主题的问题。所以我想在这里分享一下。谢谢分享。但是如果你想回答你自己的问题,请用QA的方式来回答,也就是说,在答案中加入一个答案。同样像
//table/tbody/tr/td[3]/div/div/div
或/div[2]/div[1]/div/div/div[1]
这样的XPath也很可怕,请使用更好的东西。现在,如果你能把它变成一个标准的“页面对象”,那会给人留下深刻印象。就目前的情况来看,它不太适合其他人使用。如果你只想分享,发布GitHub的要点,然后将代码嵌入到你的博客中。你可以使用google Sheets api v4访问google电子表格,这非常简单,不需要太多代码。Google Sheets API使用OAuth 2.0协议进行身份验证和授权查看以下网站,例如如何使用Google Sheets最新API集成硒测试电子表格。