Selenium is a popular software testing framework for web apps. Available programming languages like Java, C#, Groovy, Perl, PHP, Python and Ruby.

In this tutorial we will use Selenium WebDriver to take screenshots. First, Install selenium using pip install selenium

This snippet will just open your Firefox browser and loads the specified URL and save the snapshot to current directory where script is executed.


from selenium import webdriver

driver = webdriver.Firefox()
driver.get('http://www.python.org/')
driver.save_screenshot('python_org.png')
driver.quit()

This snippet will open your Firefox browser and loads the specified URL and saves the snapshot to the current directory and closes the browser.

from selenium import webdriver
import contextlib

@contextlib.contextmanager
def quitting(thing):
    yield thing
    thing.quit()

with quitting(webdriver.Firefox()) as driver:
    driver.implicitly_wait(10)
    driver.get('http://www.python.org/')
    driver.get_screenshot_as_file('python_org.png')