Take a screenshot with Selenium WebDriver
Does anyone know if it's possible to take a screenshot using Selenium WebDriver? (Note: Not Selenium RC)
Java
Yes, it is possible. The following example is in Java:
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com/");
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
// Now you can do whatever you need to do with it, for example copy somewhere
FileUtils.copyFile(scrFile, new File("c:tmpscreenshot.png"));
Python
Each WebDriver has a .save_screenshot(filename)
method. So for Firefox, it can be used like this:
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://www.google.com/')
browser.save_screenshot('screenie.png')
browser.quit()
Confusingly, a .get_screenshot_as_file(filename)
method also exists that does the same thing.
There are also methods for: .get_screenshot_as_base64()
(for embedding in html) and .get_screenshot_as_png()
(for retrieving binary data).
and Note that WebElements have a .screenshot()
method that works similarly, but only captures the selected element.
C#
public void TakeScreenshot()
{
try
{
Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();
ss.SaveAsFile(@"D:ScreenshotsSeleniumTestingScreenshot.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
throw;
}
}
链接地址: http://www.djcxy.com/p/62560.html