# https://stackoverflow.com/questions/41721734/take-screenshot-of-full-page-with-selenium-python-with-chromedriver/57338909#57338909

from selenium import webdriver
from PIL import Image

URL = 'https://www.trafficdelays.co.uk/m62-traffic-delays/'

options = webdriver.ChromeOptions()
options.headless = True

driver = webdriver.Chrome(options=options)
driver.get(URL)


def setWidth(var, adj=0):
    script = "return document.body.parentNode.scroll" + (var)
    return driver.execute_script(script) + adj


driver.set_window_size(setWidth('Width'), setWidth('Height'))


driver.find_element_by_tag_name('body').screenshot('web_screenshot.png')
# driver.find_element_by_css_selector('#post-4706').screenshot('web_screenshot.png')

# print(driver.find_element_by_css_selector('#post-4706').text)

im = Image.open('web_screenshot.png')
width, height = im.size
region = im.crop((0, 0, width, 880))
region.save('cropped.png')
driver.quit()
