title: How to limit screenshot height in selenium python?
body: <p>I would like to get a screenshot of a full web page with Selenium python. To do that I used <a href="https://stackoverflow.com/a/52572919/832230">this answer</a>.</p>
  
  <p>But now I would like to divide the screenshot and limit the height of the resulting screenshot to 30 000px. For example, if a webpage height is 40 000px, I would like a first 30 000px screenshot and then a 10 000 px screenshot.</p>
  
  <p>The solution should not be to save the full page and then crop the image, because I need it to work for very long webpages. Indeed it is not possible to get a screenshot with 120 000px height or you get this kind of error :</p>
  
  <pre><code>    raise exception_class(message, screen, stacktrace)
  selenium.common.exceptions.WebDriverException: Message: [Exception... "Failure"  nsresult: "0x80004005 (NS_ERROR_FAILURE)"  location: "JS frame :: chrome://marionette/content/capture.js :: capture.canvas :: line 154"  data: no]
  </code></pre>
  
  <p>I tried this but it does not wok at all :</p>
  
  <pre><code>from selenium import webdriver
  from selenium.webdriver.firefox.options import Options
  from time import sleep
  
  def save_screenshot(d, path):
      max_size = 30000
      original_size = d.get_window_size()
      required_width = d.execute_script('return document.body.parentNode.scrollWidth')+74
      required_height = d.execute_script('return document.body.parentNode.scrollHeight')+74
      if required_height &lt;= max_size:
          d.set_window_size(required_width, required_height)
          d.find_element_by_tag_name('body').screenshot(path)  # avoids scrollbar
      else :
          for i in range(0,int(required_height/max_size)):
              d.set_window_position(0,max_size*i)
              d.set_window_size(required_width, max_size)
              d.find_element_by_tag_name('body').screenshot(path.replace(".png",str(i+1)+".png"))
          d.set_window_position(0,max_size*int(required_height/max_size))
          d.set_window_size(required_width, required_height%max_size)
          d.find_element_by_tag_name('body').screenshot(path.replace(".png",str(int(required_height/max_size)+1)+".png"))
          d.set_window_position(0,0)
      d.set_window_size(original_size['width'], original_size['height'])
  
  if __name__ == '__main__':
      options = Options()
      options.headless = True
      driver = webdriver.Firefox(options=options,executable_path=r"C:/Users/path/geckodriver.exe")
      driver.get("http://www.lwb.com/")
      sleep(3)
      save_screenshot(driver,"C:/Users/path/test.png")
      driver.close()
  </code></pre>
  
  <p>Can someone help me here please ?</p>
  
  <p>Thank you,</p>
  
  <p>Mylha</p>