I am new to using Selenium. I have to take a screenshot of a simple webpage but it shows the error below even though I set the height already.
selenium.common.exceptions.WebDriverException: Message: unknown error: unhandled inspector error: {"code":-32000,"message":"Cannot take screenshot with 0 height."} (Session info: chrome=130.0.6723.58)
My main.py
is written as follows:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument('--disable-gpu')
driver = webdriver.Chrome(options=chrome_options)
url = 'https://www.google.com'
driver.get(url)
desired_width = 1920
desired_height = 1080
page_body = driver.find_element(By.TAG_NAME,"body")
time.sleep(20)
page_body.screenshot("page_screenshot.png")
driver.quit()
I tried adding # chrome_options.add_argument("--window-size=1920,1080")
.
I also tried having my desired_height
var set to driver.execute_script("return Math.max(document.body.scrollHeight,document.body.offsetHeight,document.documentElement.client.Height,document.documentElement.scrollHeight,document.documentElement.offsetHeight);")
I looked for other articles over here and found a similar issue was asked but the webpage the webdriver was trying to get has captchas which is not the same as mine. I literally only require to take a screenshot of a simple html webpage (assuming like google.com) without any captchas or fancy security things displayed on the webpage.
To set size, try this
driver.set_window_size(1920, 1080)
or
options.add_argument('window-size=1920x1480')
and take screenshot from driver
driver.save_screenshot("file_name.png")