0

I have been trying to scroll down an iframe to take screenshots, and thought I found a solution but I am running into a problem where scrolling 500 pixels or 3000 pixels gives me the same results (as well as scrolling by 100, but scrolling by 1 gives me the top of the page so it looked normal for a bit)

The solution was taken from this question, and my current code is as follows:(added the screenshot code just in case, though it doesnt actually reside inside the same def.

def scroll_iframe(self):
    iframe = self.driver.find_element_by_id('js-map-iframe')
    self.driver.switch_to.frame(iframe)
    self.driver.execute_script("window.scrollTo(0,3000);")
    time.sleep(5)
    self.driver.switch_to.default_content()
    time.sleep(3)
    print('iframe scroll_1')
    self.driver.set_window_size(1920, 4500)
    self.driver.save_screenshot("screenshot.png")
    print('screenshot taken')
    self.driver.quit()

I'm not too sure which part of the source code would be useful to show, but I have used find_element_by_id as the code looked like this:

<div class="pt-map-iframe-parent" style="width: 375px; height: 31804px;">
                <iframe src="long-test" frameborder="0" id="js-map-iframe" style="width: 375px; height: 31804px; transform: scale(1);"></iframe>

The iframe itself has a height of 31804 so iframe height shouldn't be a problem.. Any ideas on how to fix this, and why setting window.scrollTo(0,500) and (0,3000) would give the same result?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

1 Answers1

0

Window.scroll()

The Window.scroll() method scrolls the window to a particular place in the document. So invoking either of the methods:

Won't be effective to scroll down an to take screenshots.


Solution

An efficient approach would be to invoke scrollIntoView() for the <iframe> element as follows:

self.driver.execute_script("arguments[0].scrollIntoView();", self.driver.find_element_by_id('js-map-iframe'))
self.driver.save_screenshot("screenshot.png")

References

You can find a couple of relevant discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thank you for the response. However, I am looking to scroll through the iframe as the iframe is over 10,000 px in height, and my goal is so scroll through and take screenshots of the contents of the iframe. scrollIntoView only seems to put the iframe in view, which it has always been on the default settings anyway. Do you happen to know of a way which allows me to scroll the actual iframe itself, and not to the iframe? – AJJ Howtorot Jun 08 '20 at 01:58