硒的webdriver C#全网站截图随着ChromeDriver和FirefoxDriver [英] Selenium WebDriver C# Full Website Screenshots With ChromeDriver and FirefoxDriver

查看:2978
本文介绍了硒的webdriver C#全网站截图随着ChromeDriver和FirefoxDriver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我采取截图与ChromeDriver我得到的屏幕与我的视口的大小。
结果当我采取截图与FirefoxDriver我得到了我想要的东西,这是一个网站的全屏打印



ChromeDriver声明如下:

  IWebDriver司机=新ChromeDriver(); 



FirefoxDriver声明如下:

  IWebDriver司机=新FirefoxDriver(); 



两位车手都执行相同的代码:

  driver.Manage()Window.Maximize()。 
driver.Navigate()GoToUrl(URL); // URL是一个字符串变量
ITakesScreenshot screenshotDriver =驱动程序ITakesScreenshot;
截图截图= screenshotDriver.GetScreenshot();
screenshot.SaveAsFile(C:/test.png,ImageFormat.Png);



ChromeDriver的test.png是1920x1099分辨率,仅包含浏览器窗口。
结果FirefoxDriver的test.png是1903x16559号决议,并包含了整个页面。



我知道 GetScreenshot()方法不返回相同的分辨率大小,因为它在IEDriver,FirefoxDriver,OperaDriver,ChromeDriver稍有不同的实现。



我的问题是:




  1. 为什么会出现ChromeDriver的和FirefoxDriver的 .GetScreenshot()法之间的这种差异,即使寿它们使用相同的接口(ITakesScreenshot)?


  2. 有没有一种方法,使ChromeDriver的 GetScreenshot()方法返回整个网页画面而不是只是视口的?



解决方案

我们不能得到与ChromeDriver2整个页面截图,我们需要去手动implementation.I修改与在其正常工作与ChromeDriver博客中提供的方法。



使用此方法如下:

 私人IWebDriver _driver =新ChromeDriver(CHROME_DRIVER_PATH); 
screenshot.SaveAsFile(saveFileName,ImageFormat.Jpeg);

公共位图GetEntereScreenshot()
{

位图stitchedImage = NULL;

{
长totalwidth1 =(长)((IJavaScriptExecutor)_driver).ExecuteScript(回归document.body.offsetWidth); // documentElement.scrollWidth);

长totalHeight1 =(长)((IJavaScriptExecutor)_driver).ExecuteScript(回归document.body.parentNode.scrollHeight);

INT totalWidth =(int)的totalwidth1;
INT totalHeight =(int)的totalHeight1;

//获取视
长viewportWidth1 =(长)((IJavaScriptExecutor)_driver).ExecuteScript的尺寸(document.body的回报.clientWidth); // documentElement.scrollWidth);
长viewportHeight1 =(长)((IJavaScriptExecutor)_driver).ExecuteScript(回归window.innerHeight); // documentElement.scrollWidth);

INT viewportWidth =(int)的viewportWidth1 ;
INT viewportHeight =(int)的viewportHeight1;


//分割屏幕在多个长方形
名单<矩形>矩形=新的List<矩形>() ;
//循环直到总高度达到
的for(int i = 0; I< totalHeight; I + = viewportHeight)
{
INT newHeight = viewportHeight;
//修复如果元素的高度太大
如果(1 + viewportHeight> totalHeight)
{
newHeight = totalHeight - 我;
}
//循环,直到总宽达到
表示(中间体二= 0; II蛋白酶totalWidth;ⅱ+ = viewportWidth)
{
INT newWidth = viewportWidth;
//修正,如果元件的宽度太大
如果(ⅱ+ viewportWidth> totalWidth)
{
newWidth = totalWidth - 二;
}

//创建并添加矩形
矩形currRect =新的Rectangle(II,I,newWidth,newHeight);
rectangles.Add(currRect);
}
}

//构建映像
stitchedImage =新位图(totalWidth,totalHeight);
//获取所有的屏幕截图它们拼合在一起
矩形以前= Rectangle.Empty;
的foreach(在矩形变种矩形)
{
//计算滚动(如需要)
如果(以前!= Rectangle.Empty)
{
INT xDiff = rectangle.Right - previous.Right;
INT yDiff = rectangle.Bottom - previous.Bottom;
//滚动
//selenium.RunScript(String.Format(\"window.scrollBy({0},{1}),xDiff,yDiff));
((IJavaScriptExecutor)_driver).ExecuteScript(的String.Format(window.scrollBy({0},{1}),xDiff,yDiff));
System.Threading.Thread.Sleep(200);
}

//抓图
VAR截图=((ITakesScreenshot)_driver).GetScreenshot();

//建立形象出来的截图
图像screenshotImage的;使用(MemoryStream的memStream =新的MemoryStream(screenshot.AsByteArray))
{
screenshotImage = Image.FromStream(memStream)
;
}

//计算源矩形
矩形sourceRectangle =新的Rectangle(viewportWidth - rectangle.Width,viewportHeight - rectangle.Height,rectangle.Width,rectangle.Height);

//复制使用映像
(图形G = Graphics.FromImage(stitchedImage))
{
g.DrawImage(screenshotImage,矩形,sourceRectangle,GraphicsUnit。像素);
}

//设置上矩形
=以往矩形;
}
}
赶上(异常前)
{
//句柄
}
返回stitchedImage;
}


When I take screenshots with ChromeDriver I get screens with the size of my viewport.
When I take screenshots with FirefoxDriver I get what I want, which is a full screen print of a website.

ChromeDriver is declared like this:

IWebDriver driver = new ChromeDriver();

FirefoxDriver is declared like this:

IWebDriver driver = new FirefoxDriver();

Both drivers execute identical code:

driver.Manage().Window.Maximize();
driver.Navigate().GoToUrl(url);//url is a string variable
ITakesScreenshot screenshotDriver = driver as ITakesScreenshot;
Screenshot screenshot = screenshotDriver.GetScreenshot();
screenshot.SaveAsFile("c:/test.png", ImageFormat.Png);

ChromeDriver's test.png is of 1920x1099 resolution and contains only the browser viewport.
FirefoxDriver's test.png is of 1903x16559 resolution and contains the whole page.

I know that GetScreenshot() method doesn't return identical resolution sizes because it has slightly different implementations in IEDriver, FirefoxDriver, OperaDriver, ChromeDriver.

My questions are:

  1. Why is there such difference between ChromeDriver's and FirefoxDriver's .GetScreenshot() method, even tho they use an identical interface (ITakesScreenshot)?

  2. Is there a way to make ChromeDriver's GetScreenshot() method return the whole webpage screen instead of just the viewport?

解决方案

we can't get the entire page screenshot with ChromeDriver2, we need to go for manual implementation.I have modified a method with is available in a blog which works fine with ChromeDriver.

use this method as following :

private IWebDriver _driver = new ChromeDriver(CHROME_DRIVER_PATH);
screenshot.SaveAsFile(saveFileName, ImageFormat.Jpeg);

public Bitmap GetEntereScreenshot()
    {

        Bitmap stitchedImage = null;
        try
        {
            long totalwidth1 = (long)((IJavaScriptExecutor)_driver).ExecuteScript("return document.body.offsetWidth");//documentElement.scrollWidth");

            long totalHeight1 = (long)((IJavaScriptExecutor)_driver).ExecuteScript("return  document.body.parentNode.scrollHeight");

            int totalWidth = (int)totalwidth1;
            int totalHeight = (int)totalHeight1;

            // Get the Size of the Viewport
            long viewportWidth1 = (long)((IJavaScriptExecutor)_driver).ExecuteScript("return document.body.clientWidth");//documentElement.scrollWidth");
            long viewportHeight1 = (long)((IJavaScriptExecutor)_driver).ExecuteScript("return window.innerHeight");//documentElement.scrollWidth");

            int viewportWidth = (int)viewportWidth1;
            int viewportHeight = (int)viewportHeight1;


        // Split the Screen in multiple Rectangles
        List<Rectangle> rectangles = new List<Rectangle>();
        // Loop until the Total Height is reached
        for (int i = 0; i < totalHeight; i += viewportHeight)
        {
            int newHeight = viewportHeight;
            // Fix if the Height of the Element is too big
            if (i + viewportHeight > totalHeight)
            {
                newHeight = totalHeight - i;
            }
            // Loop until the Total Width is reached
            for (int ii = 0; ii < totalWidth; ii += viewportWidth)
            {
                int newWidth = viewportWidth;
                // Fix if the Width of the Element is too big
                if (ii + viewportWidth > totalWidth)
                {
                    newWidth = totalWidth - ii;
                }

                // Create and add the Rectangle
                Rectangle currRect = new Rectangle(ii, i, newWidth, newHeight);
                rectangles.Add(currRect);
            }
        }

        // Build the Image
        stitchedImage = new Bitmap(totalWidth, totalHeight);
        // Get all Screenshots and stitch them together
        Rectangle previous = Rectangle.Empty;
        foreach (var rectangle in rectangles)
        {
            // Calculate the Scrolling (if needed)
            if (previous != Rectangle.Empty)
            {
                int xDiff = rectangle.Right - previous.Right;
                int yDiff = rectangle.Bottom - previous.Bottom;
                // Scroll
                //selenium.RunScript(String.Format("window.scrollBy({0}, {1})", xDiff, yDiff));
                ((IJavaScriptExecutor)_driver).ExecuteScript(String.Format("window.scrollBy({0}, {1})", xDiff, yDiff));
                System.Threading.Thread.Sleep(200);
            }

            // Take Screenshot
            var screenshot = ((ITakesScreenshot)_driver).GetScreenshot();

            // Build an Image out of the Screenshot
            Image screenshotImage;
            using (MemoryStream memStream = new MemoryStream(screenshot.AsByteArray))
            {
                screenshotImage = Image.FromStream(memStream);
            }

            // Calculate the Source Rectangle
            Rectangle sourceRectangle = new Rectangle(viewportWidth - rectangle.Width, viewportHeight - rectangle.Height, rectangle.Width, rectangle.Height);

            // Copy the Image
            using (Graphics g = Graphics.FromImage(stitchedImage))
            {
                g.DrawImage(screenshotImage, rectangle, sourceRectangle, GraphicsUnit.Pixel);
            }

            // Set the Previous Rectangle
            previous = rectangle;
        }
        }
        catch (Exception ex)
        {
            // handle
        }
        return stitchedImage;
    }

这篇关于硒的webdriver C#全网站截图随着ChromeDriver和FirefoxDriver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆