Home > Enterprise >  Python Selenium Take a Screenshot Of An Whole Page Without Using Headless Mode
Python Selenium Take a Screenshot Of An Whole Page Without Using Headless Mode

Time:12-03

I need to take a screenshot of an element which is very long and not fit on the screen, I can use headless mode to do this but site doesn't allow me to do even with user-agent and other stuff.

But I can access the site with undetectedChromeDriver, so there's a extension to do this stuff called 'HTML Elements Screenshot'.

That extension will allow you to select element and take the screenshot of an whole element for you.

I automated that process with pyautogui and cv2 but I want to do it without this libraries. Is there any Javascript code to do it ? I did my research but can't find any useful. Thanks in advance

The codes I tried:

def save_screenshot(driver, path: str = 'screenshot.png') -> None:

        input('Let it go when u ready.')
        driver.switch_to.window(driver.window_handles[-1])
        original_size = driver.get_window_size()
        required_width = driver.execute_script('return document.body.parentNode.scrollWidth')
        required_height = driver.execute_script('return document.body.parentNode.scrollHeight')
        driver.set_window_size(required_width, required_height)
        driver.save_screenshot(path)  # has scrollbar
        #driver.find_element_by_tag_name('body').screenshot(path)  # avoids scrollbar
        driver.set_window_size(original_size['width'], original_size['height'])
    def saveScreenshot(driver,path: str="screenshot.png"):

        input('Let it go when u ready.')
        driver.switch_to.window(driver.window_handles[-1])
        el = driver.find_element(By.TAG_NAME,"body")
        el.screenshot(path)
        driver.quit()

CodePudding user response:

For python you can use pyppeteer.

For javascript you can use puppeteer

You can find the documentation here

  • Related