简体   繁体   中英

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

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

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM