How can I take a screenshot with Selenium WebDriver?

后端 未结 30 2595
不知归路
不知归路 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:40

    C#

    You can use the following code snippet/function to take screenshot with Selenium:

        public void TakeScreenshot(IWebDriver driver, string path = @"output")
        {
            var cantakescreenshot = (driver as ITakesScreenshot) != null;
            if (!cantakescreenshot)
                return;
            var filename = string.Empty + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + DateTime.Now.Millisecond;
            filename = path + @"\" + filename + ".png";
            var ss = ((ITakesScreenshot)driver).GetScreenshot();
            var screenshot = ss.AsBase64EncodedString;
            byte[] screenshotAsByteArray = ss.AsByteArray;
            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);
            ss.SaveAsFile(filename, ImageFormat.Png);
        }
    

提交回复
热议问题