是否可以使用Selenium WebDriver进行截图?

(注:不含硒遥控器)


当前回答

Java

对于较新版本的Java, FileUtils函数不起作用。在这里,下面的功能可以完美地复制屏幕截图。

import java.io.File;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.io.FileHandler;

File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
File destfile = new File(destination folder /filename.extension);
FileHandler.copy(scrfile, destfile);

其他回答

Python

每个WebDriver都有一个. save_截图(filename)方法。所以对于Firefox,它可以这样使用:

from selenium import webdriver

browser = webdriver.Firefox()
browser.get('http://www.google.com/')
browser.save_screenshot('screenie.png')

令人困惑的是,.get_screenshot_as_file(filename)方法也存在,它做同样的事情。

还有一些方法:.get_screenshot_as_base64()(用于嵌入HTML)和.get_screenshot_as_png()(用于检索二进制数据)。

注意,WebElements有一个.截图()方法,它的工作原理类似,但只捕获所选元素。

Python

您可以使用Python web驱动程序从windows中捕获图像。使用下面的代码哪个页面需要捕获屏幕截图。

driver.save_screenshot('c:\foldername\filename.extension(png, jpeg)')

我使用下面的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# (Ranorex API)

public static void ClickButton()
{
    try
    {
        // code
    }
    catch (Exception e)
    {
        TestReport.Setup(ReportLevel.Debug, "myReport.rxlog", true);
        Report.Screenshot();
        throw (e);
    }
}

Java

String yourfilepath = "E:\\username\\Selenium_Workspace\\foldername";

// Take a snapshort
File snapshort_file = ((TakesScreenshot) mWebDriver)
        .getScreenshotAs(OutputType.FILE);
// Copy the file into folder

FileUtils.copyFile(snapshort_file, new File(yourfilepath));