r/selenium • u/MasterofDisgoise69 • Aug 03 '20
How do I take a screenshot of a page displayed in a headless browser?
So I've made a code in python that runs headless chrome, adds my ID no and name, then displays a particular page. I want to take the screenshot of this page. The code is as follows:
import bs4, requests
from selenium import webdriver
optionset= webdriver.ChromeOptions()
optionset.add_argument("headless")
browser = webdriver.Chrome (executable.path='chromedriver path', options=optionset)
browser.get("webpage'')
dataID = browser.find_element_by_name ('regno')
dataID.send_keys(key')
datapass = browser. find element_by_name ('name')
datapass. send_keys ("myname')
datapass.submit()
The code works fine in a normal tab and displays the page. But I want it to run it headless and take the screenshot. Is there a way I can take a screenshot and save it?
Edit: Solved it by simply adding browser.save_screenshot("path//screenshot.png"). I'm stupid. Have a nice day!