0

I need to scroll into a DIV for printing the entire page.

I already tried SendKeys at the DIV element, but it scrolled the entire page. I don´t want a solution to scroll to a defined object because I have many different screens with this attributes with different elements.

The DIV is:

<div class="content modal-overflow">

The CSS Class of it:

  .modal-overflow
 {  
  max-height:430px;
  overflow:auto
 }

430px is the visible part of the DIV. The entire DIV has 750px.

The code:

 <div class="modal fade" id="modalDetalhes" tabindex="-1" role="dialog">
      <div class="modal-dialog width750">
          <div class="modal-content">
            <div class="modal-header">
               </span></button>
            </div>
            <div class="modal-body">
                    (...)

                <div class="content modal-overflow">
                    <div class="row">
                        <div class="col-md-3">
                            <div class="form-group">
                                (...)

Today I use a function to Get Entire Screenshot. I used this solution: Selenium WebDriver C# Full Website Screenshots With ChromeDriver and FirefoxDriver

How can I do this scroll printing the Entire DIV?

Carol
  • 163
  • 2
  • 10

1 Answers1

0

Here is the method to take the screenshot of the particular element.

public static void TakeElementScreenshot(IWebDriver driver, IWebElement element)
{
    try
    {
        string screenshotName = DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + "element_screenshot.jpg";
        Byte[] byteArray = ((ITakesScreenshot)driver).GetScreenshot().AsByteArray;
        System.Drawing.Bitmap screenshot = new System.Drawing.Bitmap(new System.IO.MemoryStream(byteArray));
        System.Drawing.Rectangle eleImage = new System.Drawing.Rectangle(element.Location.X, element.Location.Y, element.Size.Width, element.Size.Height);
        screenshot = screenshot.Clone(eleImage, screenshot.PixelFormat);
        screenshot.Save(String.Format(@"here goes the folder path" + screenshotName, System.Drawing.Imaging.ImageFormat.Jpeg));
    }
    catch (Exception e)
    {
        logger.Error(e.StackTrace + ':' + e.Message);
    }
}

If you have to scroll to the element then use the below.

var elem = driver.FindElement(By.ClassName("content modal-overflow"));
driver.ExecuteScript("arguments[0].scrollIntoView(true);", elem);

Not sure what you mean by " I don´t want a solution to scroll to a defined object because I have many diferent screens with this attributes with diferent elements.". Do you want to scorll the contents in the modal window?

supputuri
  • 13,644
  • 2
  • 21
  • 39
  • It works. But it isn´t exactly what I need. With this solution I need to know the first element that isn´t in the viewport to scroll until then to mount the print and it´s dificult to be assertive to have a fluid image. I need a solution with coordinates points (or something like that) to calculate the height of the viewport and mount the entire image. In other words, a need to generate only one image with the entire screenshot of the class with overflow. – Carol Mar 20 '19 at 12:45
  • Ok, Let me update the answer. I am giving a method called `TakeElementScreenshot`. You have to pass `driver` and `element` as params to this method call. – supputuri Mar 20 '19 at 13:22
  • Almost there! Exception thrown: 'System.OutOfMemoryException' in System.Drawing.dll em System.Drawing.Bitmap.Clone(Rectangle rect, PixelFormat format) – Carol Mar 20 '19 at 16:17
  • hmm. Have to check if we can use other library . – supputuri Mar 20 '19 at 16:35
  • I´ve already tested your solution changing the coordinates of the rectangle for real numbers. In this way, the code worked, but when I tried to put the height including the part that was hiding by the scroll, I get this Excpetion of out of memory. In other words this code will not work for DIV Entire Screeshot, just for the visible part of the DIV at the screen. – Carol Mar 21 '19 at 12:36