Selenium take screenshot
Selenium is a web automation framework that can be used for automated testing, web scraping and anything you can do with a web browser. We can use Selenium to take automated screenshots of a webpage.
Related course:
Practice Python with interactive exercises
Take screenshot Selenium
Selenium will start the given web browser and open the page. It will then take a screenshot and save it to the local hard disk.
take screenshot using python codeWe start a web driver (Chromium) and open the webpage python.org. Then we call the method:
driver.save_screenshot("screenshot.png")
The screenshot will be saved in the same directory as the program: the program path.
The full code:
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.add_argument("--test-type")
options.binary_location = "/usr/bin/chromium"
driver = webdriver.Chrome(chrome_options=options)
driver.get('https://python.org')
driver.save_screenshot("screenshot.png")
driver.close()
Remember to call driver.close() otherwise the browser stays open after the program finishes.