是否可以使用Selenium WebDriver进行截图?
(注:不含硒遥控器)
是否可以使用Selenium WebDriver进行截图?
(注:不含硒遥控器)
当前回答
Jython
import org.openqa.selenium.OutputType as OutputType
import org.apache.commons.io.FileUtils as FileUtils
import java.io.File as File
import org.openqa.selenium.firefox.FirefoxDriver as FirefoxDriver
self.driver = FirefoxDriver()
tempfile = self.driver.getScreenshotAs(OutputType.FILE)
FileUtils.copyFile(tempfile, File("C:\\screenshot.png"))
其他回答
我使用下面的c#代码来获取整个页面或仅一个浏览器截图
public void screenShot(string tcName)
{
try
{
string dateTime = string.Format("{0:yyyy-MM-dd_hh-mm-ss-tt}", DateTime.Now);
string screenShotName = @"D:\Selenium\Project\VAM\VAM\bin" + "\\" + tcName + dateTime + ".png";
ITakesScreenshot screen = driverScript.driver as ITakesScreenshot;
Screenshot screenshot = screen.GetScreenshot();
screenshot.SaveAsFile(screenShotName, System.Drawing.Imaging.ImageFormat.Png);
if (driverScript.last == 1)
this.writeResult("Sheet1", "Fail see Exception", "Status", driverScript.resultRowID);
}
catch (Exception ex)
{
driverScript.writeLog.writeLogToFile(ex.ToString(), "inside screenShot");
}
}
public void fullPageScreenShot(string tcName)
{
try
{
string dateTime = string.Format("{0:yyyy-MM-dd_hh-mm-ss-tt}", DateTime.Now);
string screenShotName = @"D:\Selenium\Project\VAM\VAM\bin" + "\\" + tcName + dateTime + ".png";
Rectangle bounds = Screen.GetBounds(Point.Empty);
using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
{
using (Graphics g = Graphics.FromImage(bitmap))
{
g.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size);
}
bitmap.Save(screenShotName, System.Drawing.Imaging.ImageFormat.Png);
}
if (driverScript.last == 1)
this.writeResult("Sheet1", "Pass", "Status", driverScript.resultRowID);
}
catch (Exception ex)
{
driverScript.writeLog.writeLogToFile(ex.ToString(), "inside fullPageScreenShot");
}
}
C#
public void TakeScreenshot()
{
try
{
Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();
ss.SaveAsFile(@"D:\Screenshots\SeleniumTestingScreenshot.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
throw;
}
}
更新2022
要在Selenium中截取屏幕截图,我们使用一个名为TakesScreenshot的接口,它使Selenium WebDriver能够捕捉屏幕截图并以不同的方式存储它。它有一个getScreenshotAs()方法,用于捕获屏幕截图并将其存储在指定的位置。
//Convert webdriver to TakeScreenshot
File screenshotFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
在上面的代码中,它将WebDriver对象(驱动程序)转换为TakeScreenshot。并调用getScreenshotAs()方法通过提供参数*OutputType.FILE来创建图像文件。
我们可以使用File对象将图像复制到我们想要的位置,如下所示,使用FileUtils类。
FileUtils.copyFile(screenshotFile , new File("C:\\temp\\screenshot.png));
捕获整个页面
Selenium WebDriver没有提供捕获整个页面截图的固有功能。为了捕获整个页面的截图,我们必须使用一个名为shot的第三方库。它提供了截取特定WebElement的截图以及整页截图的功能。
捕获屏幕大小的图像
Screenshot screenshot = new Ashot().takeScreenshot(driver);
捕获整个页面的截图
Screenshot s=new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000)).takeScreenshot(driver);
ImageIO.write(s.getImage(),"PNG",new File("<< file path>>"));
在上面的代码中,1000是以毫秒为单位的滚动时间。换句话说,这意味着该程序将滚动每1000毫秒来截取屏幕截图。
捕获一个元素
在Selenium中有两种方法来捕获web元素的屏幕截图。
取全屏图像,然后根据网页元素的尺寸裁剪图像。 在web元素上使用getScreenshotAs()方法。(这只在selenium版本4.X中可用)
是的,可以通过Selenium WebDriver来截屏。我目前使用Chrome驱动来抓取网站图片。请参考以下方法captureScreenshot()。
您还可以添加对web驱动程序的限制,例如
使用无头版的网页浏览器 当页面加载时禁用通知 启动全屏等。
如果一个网站配备了警报框,你的网页驱动程序将无法捕捉屏幕截图,因为异常将被抛出。在这种情况下,你需要关闭警告框,然后获取截图。下面的代码片段关闭警报框。
public void captureScreenshot() throws InterruptedException, IOException {
System.out.println("Creating Chrome Driver");
// Set Chrome Driver
System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
// Add arguments to Chrome Options
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--headless");
chromeOptions.addArguments("start-maximized");
chromeOptions.addArguments("--disable-gpu");
chromeOptions.addArguments("--start-fullscreen");
chromeOptions.addArguments("--disable-extensions");
chromeOptions.addArguments("--disable-popup-blocking");
chromeOptions.addArguments("--disable-notifications");
chromeOptions.addArguments("--window-size=1920,1080");
chromeOptions.addArguments("--no-sandbox");
chromeOptions.addArguments("--dns-prefetch-disable");
chromeOptions.addArguments("enable-automation");
chromeOptions.addArguments("disable-features=NetworkService");
WebDriver driver = new ChromeDriver(chromeOptions);
driver.get("https://www.google.com");
System.out.println("Wait a bit for the page to render");
TimeUnit.SECONDS.sleep(5);
System.out.println("Taking Screenshot");
File outputFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
String imageDetails = "D:\\Images";
File screenShot = new File(imageDetails).getAbsoluteFile();
FileUtils.copyFile(outputFile, screenShot);
System.out.println("Screenshot saved: {}" + imageDetails);
}
}
为了接受警报框和获取截图:
String alertText = alert.getText();
System.out.println("ERROR: (ALERT BOX DETECTED) - ALERT MSG : " + alertText);
alert.accept();
File outputFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
String imageDetails = "D://Images"
File screenShot = new File(imageDetails).getAbsoluteFile();
FileUtils.copyFile(outputFile, screenShot);
System.out.println("Screenshot saved: {}" + imageDetails);
driver.close();
c#代码
IWebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com/");
File scrFile = ((ITakesScreenshot)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:\\tmp\\screenshot.png"));