'How to get screenshot of the complete cookie banner of webpage in selenium python

enter image description here

I am able to get the full screenshot of the webpage but not the cookie banner.Can i do it in selenium or i should use any other tool



Solution 1:[1]

To get a screenshot of the complete banner, you have to enlarge the banner so that all the scrollable content is visible. However, if your computer screen resolution is 1920x1080 or lower, the complete banner will not fit your screen, so in this case you have to use the browser in headless mode. In this mode you can resize the browser to whatever desired size. This is the working code

import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service

options = webdriver.ChromeOptions()
options.add_argument("--headless")

chromedriver_path = '...'
driver = webdriver.Chrome(options=options, service=Service(chromedriver_path))

driver.get('https://www.pcgamer.com/uk/')
time.sleep(2)
# click the button "MORE OPTIONS"
driver.find_element(By.XPATH, "//button[contains(.,'MORE OPTIONS')]").click()
time.sleep(2)
# resize the browser window
driver.set_window_size(1300, 1400)
banner = driver.find_element(By.CSS_SELECTOR, 'div[role=dialog]')
# resize the banner
driver.execute_script("arguments[0].setAttribute('style', 'max-height: 1100px;')", banner)
time.sleep(1)
banner.screenshot('banner_screenshot.png')

and this is the image you will obtain

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 sound wave