How can I take a screenshot with Selenium WebDriver?

后端 未结 30 2613
不知归路
不知归路 2020-11-21 07:48

Is it possible to take a screenshot using Selenium WebDriver?

(Note: Not Selenium Remote Control)

相关标签:
30条回答
  • 2020-11-21 08:15

    C#

    public Bitmap TakeScreenshot(By by) {
        // 1. Make screenshot of all screen
        var screenshotDriver = _selenium as ITakesScreenshot;
        Screenshot screenshot = screenshotDriver.GetScreenshot();
        var bmpScreen = new Bitmap(new MemoryStream(screenshot.AsByteArray));
    
        // 2. Get screenshot of specific element
        IWebElement element = FindElement(by);
        var cropArea = new Rectangle(element.Location, element.Size);
        return bmpScreen.Clone(cropArea, bmpScreen.PixelFormat);
    }
    
    0 讨论(0)
  • 2020-11-21 08:15

    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"))
    
    0 讨论(0)
  • 2020-11-21 08:16

    Java

    Seems to be missing here - taking screenshot of a specific element in Java:

    public void takeScreenshotElement(WebElement element) throws IOException {
        WrapsDriver wrapsDriver = (WrapsDriver) element;
        File screenshot = ((TakesScreenshot) wrapsDriver.getWrappedDriver()).getScreenshotAs(OutputType.FILE);
        Rectangle rectangle = new Rectangle(element.getSize().width, element.getSize().height);
        Point location = element.getLocation();
        BufferedImage bufferedImage = ImageIO.read(screenshot);
        BufferedImage destImage = bufferedImage.getSubimage(location.x, location.y, rectangle.width, rectangle.height);
        ImageIO.write(destImage, "png", screenshot);
        File file = new File("//path//to");
        FileUtils.copyFile(screenshot, file);
    }
    
    0 讨论(0)
  • 2020-11-21 08:17

    Java

    I could not get the accepted answer to work, but as per the current WebDriver documentation, the following worked fine for me with Java 7 on OS X v10.9 (Mavericks):

    import java.io.File;
    import java.net.URL;
    
    import org.openqa.selenium.OutputType;
    import org.openqa.selenium.TakesScreenshot;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.remote.Augmenter;
    import org.openqa.selenium.remote.DesiredCapabilities;
    import org.openqa.selenium.remote.RemoteWebDriver;
    
    public class Testing {
    
       public void myTest() throws Exception {
           WebDriver driver = new RemoteWebDriver(
                   new URL("http://localhost:4444/wd/hub"),
                   DesiredCapabilities.firefox());
    
           driver.get("http://www.google.com");
    
           // RemoteWebDriver does not implement the TakesScreenshot class
           // if the driver does have the Capabilities to take a screenshot
           // then Augmenter will add the TakesScreenshot methods to the instance
           WebDriver augmentedDriver = new Augmenter().augment(driver);
           File screenshot = ((TakesScreenshot)augmentedDriver).
                   getScreenshotAs(OutputType.FILE);
       }
    }
    
    0 讨论(0)
  • 2020-11-21 08:19

    C# (Ranorex API)

    public static void ClickButton()
    {
        try
        {
            // code
        }
        catch (Exception e)
        {
            TestReport.Setup(ReportLevel.Debug, "myReport.rxlog", true);
            Report.Screenshot();
            throw (e);
        }
    }
    
    0 讨论(0)
  • 2020-11-21 08:20

    Java (Robot Framework)

    I used this method for taking a screenshot.

    void takeScreenShotMethod(){
        try{
            Thread.sleep(10000)
            BufferedImage image = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
            ImageIO.write(image, "jpg", new File("./target/surefire-reports/screenshot.jpg"));
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }
    

    You may use this method wherever required.

    0 讨论(0)
提交回复
热议问题