How can I take a screenshot with Selenium WebDriver?

后端 未结 30 2590
不知归路
不知归路 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);
    }
    

提交回复
热议问题