<small id='Dt5dD'></small><noframes id='Dt5dD'>

    1. <i id='Dt5dD'><tr id='Dt5dD'><dt id='Dt5dD'><q id='Dt5dD'><span id='Dt5dD'><b id='Dt5dD'><form id='Dt5dD'><ins id='Dt5dD'></ins><ul id='Dt5dD'></ul><sub id='Dt5dD'></sub></form><legend id='Dt5dD'></legend><bdo id='Dt5dD'><pre id='Dt5dD'><center id='Dt5dD'></center></pre></bdo></b><th id='Dt5dD'></th></span></q></dt></tr></i><div id='Dt5dD'><tfoot id='Dt5dD'></tfoot><dl id='Dt5dD'><fieldset id='Dt5dD'></fieldset></dl></div>

      <tfoot id='Dt5dD'></tfoot>
    2. <legend id='Dt5dD'><style id='Dt5dD'><dir id='Dt5dD'><q id='Dt5dD'></q></dir></style></legend>
        <bdo id='Dt5dD'></bdo><ul id='Dt5dD'></ul>

      1. Selenium WebDriver C# 使用 ChromeDriver 和 FirefoxDriver 的完整网站截

        时间:2023-09-14

        <i id='1e0EG'><tr id='1e0EG'><dt id='1e0EG'><q id='1e0EG'><span id='1e0EG'><b id='1e0EG'><form id='1e0EG'><ins id='1e0EG'></ins><ul id='1e0EG'></ul><sub id='1e0EG'></sub></form><legend id='1e0EG'></legend><bdo id='1e0EG'><pre id='1e0EG'><center id='1e0EG'></center></pre></bdo></b><th id='1e0EG'></th></span></q></dt></tr></i><div id='1e0EG'><tfoot id='1e0EG'></tfoot><dl id='1e0EG'><fieldset id='1e0EG'></fieldset></dl></div>
            <legend id='1e0EG'><style id='1e0EG'><dir id='1e0EG'><q id='1e0EG'></q></dir></style></legend>

            <small id='1e0EG'></small><noframes id='1e0EG'>

                <tbody id='1e0EG'></tbody>

            • <tfoot id='1e0EG'></tfoot>
              • <bdo id='1e0EG'></bdo><ul id='1e0EG'></ul>
                  本文介绍了Selenium WebDriver C# 使用 ChromeDriver 和 FirefoxDriver 的完整网站截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  当我使用 ChromeDriver 截屏时,我得到的屏幕与我的视口大小相同.
                  当我使用 FirefoxDriver 截屏时,我得到了我想要的,这是一个网站的全屏打印.

                  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 是这样声明的:

                  ChromeDriver is declared like this:

                  IWebDriver driver = new ChromeDriver();
                  

                  FirefoxDriver 声明如下:

                  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 的 test.png 分辨率为 1920x1099,仅包含浏览器视口.
                  FirefoxDriver 的 test.png 分辨率为 1903x16559,包含整个页面.

                  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.

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

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

                  我的问题是:

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

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

                  有没有办法让 ChromeDriver 的 GetScreenshot() 方法返回整个网页屏幕而不仅仅是视口?

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

                  推荐答案

                  ChromeDriver2无法获取整个页面截图,需要手动实现.我在博客中修改了一个方法,可以使用ChromeDriver 很好.

                  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.

                  使用此方法如下:

                  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;
                      }
                  

                  这篇关于Selenium WebDriver C# 使用 ChromeDriver 和 FirefoxDriver 的完整网站截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:OpenQA.Selenium.WebDriverException:未知错误:Chrome 无法启动:在 Linux 下一篇:即使元素存在,ExpectedConditions.ElementIsVisible 也会返回 TimeoutExcep

                  相关文章

                    1. <legend id='OWgdj'><style id='OWgdj'><dir id='OWgdj'><q id='OWgdj'></q></dir></style></legend>

                      <tfoot id='OWgdj'></tfoot>
                        <bdo id='OWgdj'></bdo><ul id='OWgdj'></ul>

                      <i id='OWgdj'><tr id='OWgdj'><dt id='OWgdj'><q id='OWgdj'><span id='OWgdj'><b id='OWgdj'><form id='OWgdj'><ins id='OWgdj'></ins><ul id='OWgdj'></ul><sub id='OWgdj'></sub></form><legend id='OWgdj'></legend><bdo id='OWgdj'><pre id='OWgdj'><center id='OWgdj'></center></pre></bdo></b><th id='OWgdj'></th></span></q></dt></tr></i><div id='OWgdj'><tfoot id='OWgdj'></tfoot><dl id='OWgdj'><fieldset id='OWgdj'></fieldset></dl></div>

                      <small id='OWgdj'></small><noframes id='OWgdj'>