Skip to content

Instantly share code, notes, and snippets.

@narita1980
Created June 5, 2012 09:32
Show Gist options
  • Save narita1980/2873883 to your computer and use it in GitHub Desktop.
Save narita1980/2873883 to your computer and use it in GitHub Desktop.
WebDriverを使ったGoogle検索結果画面の取得方法
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
public class SampleWebDriver {
public static void main(String args[]) {
DesiredCapabilities ieCap = DesiredCapabilities.internetExplorer();
ieCap.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);
WebDriver driver = new InternetExplorerDriver(ieCap);
driver.get("http://www.google.co.jp/");
WebElement elem = driver.findElement(By.name("q"));
elem.sendKeys("検索キーワード");
elem.submit();
(new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return true;
}
});
File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(scrFile, new File("result.png"));
} catch (IOException e) {
e.printStackTrace();
}
driver.quit();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment