How to save mobile screenshot using Headless Google Chrome with Selenium How to save mobile screenshot using Headless Google Chrome with Selenium selenium selenium

How to save mobile screenshot using Headless Google Chrome with Selenium


If you want to take a full-screen screenshot of the webpage, you can use this.

The description of the problem you are experiencing isn't clear, so I'm assuming you want to hide the scroll bars and prevent them from appearing on the screenshot Selenium produces.

"""Take a full-page screenshot of the browser"""# Save the original window size so we can restore it lateroriginal = self.driver.get_window_size()# Format the original in a tuple object we can use lateroriginal = (original['width'], original['height'],)# Get the height that's needed in order to fully render the pagenewheight = int(self.driver.execute_script("""return Math.max(    document.body.scrollHeight,    document.body.offsetHeight,    document.documentElement.clientHeight,    document.documentElement.scrollHeight,    document.documentElement.offsetHeight);"""))# Set the new height# Most responsive webpages handle width flawlessly, so you can use a constant width# You can also set it dynamically if you wishself.driver.set_window_size(1000, newheight)# Hide the main scrollbar using some css tricks# You can also inject a# <style>* {overflow: hidden}</style># to hide all scrollbars if you want self.driver.execute_script("""document.body.parentElement.style.overflow = "hidden";""")# b64ss = self.driver.get_screenshot_as_base64()# print(b64ss)# The screenshot must be saved to a file because base64 has# size restrictions that usually cause an incomplete screenshotself.driver.save_screenshot('mobile_fullpage_screenshot.png')# Restore the original window sizesself.driver.set_window_size(*original)