can anyone tell me how I can capture a webpage using Java Selenium? With an exampl开发者_运维百科e....
See here: Capturing screenshots from remote Selenium RC.
In essence:
"To solve this you can use the captureScreenshotToString and captureEntirePageScreenshotToString commands, which return a Base64 encoded String of the screenshot, which you can then decode and save to disk on your testrunner machine."
I think this is what you are looking for. But try to be mores specific if it is not.
captureEntirePageScreenshot ( filename,kwargs ) Saves the entire contents of the current window canvas to a PNG file. Contrast this with the captureScreenshot command, which captures the contents of the OS viewport (i.e. whatever is currently being displayed on the monitor), and is implemented in the RC only. Currently this only works in Firefox when running in chrome mode, and in IE non-HTA using the EXPERIMENTAL "Snapsie" utility. The Firefox implementation is mostly borrowed from the Screengrab! Firefox extension. Please see http://www.screengrab.org and http://snapsie.sourceforge.net/ for details.
Arguments: * filename - the path to the file to persist the screenshot as. No
filename extension will be appended by default. Directories will not be created if they do not exist, and an exception will be thrown, possibly by native code. * kwargs - a kwargs string that modifies the way the screenshot is captured. Example: "background=#CCFFDD" . Currently valid options:
background the background CSS for the HTML document. This may be useful
to set for capturing screenshots of less-than-ideal layouts, for example where absolute positioning causes the calculation of the canvas dimension to fail and a black background is exposed (possibly obscuring black text).
public static void getSnapShot(WebDriver driver, String event) {
{
try {
File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
BufferedImage originalImage = ImageIO.read(scrFile);
//int type = originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : originalImage.getType();
BufferedImage resizedImage = CommonUtilities.resizeImage(originalImage, IMG_HEIGHT, IMG_WIDTH);
ImageIO.write(resizedImage, "jpg", new File(path + "/"+ testCaseId + "/img/" + index + ".jpg"));
Image jpeg = Image.getInstance(path + "/" + testCaseId + "/img/"+ index + ".jpg");
jpeg.setAlignment(Image.MIDDLE);
++index;
} catch (Exception e) {
e.printStackTrace();
}
}
}
I like to use PhantomJS driver for taking screenshots.
public class Test {
public static void main(String[] args) {
//PhantomJS headless driver
File file = new File("D:\\Webdriver\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe");
System.setProperty("phantomjs.binary.path", file.getAbsolutePath());
WebDriver driver = new PhantomJSDriver();
//Set Size here
driver.manage().window().setSize(new Dimension(1600,900));
//To wait until the element get visible or invisible.
WebDriverWait wait = new WebDriverWait(driver, 25);
//To access url.
driver.get("https://www.google.co.in");
//For wait until the element get visible.
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("lst-ib")));
File shot=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(shot, new File("D:\\Webdriver\\Capture.jpg"));
}
}
It takes full page screenshot of chrome webpage.
System.setProperty("webdriver.chrome.driver", "F:\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized");
WebDriver driver = new ChromeDriver(options);
String baseUrl = "https://www.google.co.in";
driver.get(baseUrl);
String fullscreen =Keys.chord(Keys.F11);
driver.findElement(By.cssSelector("body")).sendKeys(fullscreen);
TakesScreenshot scrShot =((TakesScreenshot)driver);
File SrcFile=scrShot.getScreenshotAs(OutputType.FILE);
File DestFile=new File("F://test.png");
FileUtils.copyFile(SrcFile, DestFile);
driver.close();
I see a lot of answers explaining screenshots, but just incase you are asking how to get the entire source of the page use the following method:
String pageSource = driver.getPageSource();
Here is a runnable example.
精彩评论