In this Python Selenium screenshot tutorial, we are going to explore different ways of taking screenshots using Selenium’s Python bindings.
we need a driver to proceed with clicking Python Selenium screenshots of webpages.
You can choose any browser of your choice, and you can download the drivers from the following links :
Let’s understand what we are doing here.
from selenium import webdriver >> This line imports the WebDriver which we use to fire-up a browser instance and use APIs to interact with web elements.
from time import sleep >> his line imports the sleep function from Python’s ‘time’ module. This accepts integer arguments which equals the number of seconds. The script waits for the specified number of seconds before executing the next line of code.
browser = webdriver.chrome(executable_path) This line is equivalent to saying, use the keyword ‘browser’ as you would use ‘webdriver.chrome(executable_path)’.
browser.get(“https://www.python.org/”)
browser.quit() >> Lastly, the browser needs to be closed and this line does the same.
selenium
PyYAML
lazy-load
We now demonstrate how we can use the screen_by_xpath() method to capture any element on the page.
from screenshot import ScreenShot
from datetime import datetime
if __name__ == '__main__':
screen = ScreenShot()
image_name = "{}.png".format(
datetime.now().strftime('%Y-%m-%d%H-%M-%S'))
screen.screen_by_xpath(url='https://python.org/',
xpath='//*[@id="touchnav-wrapper"]/header',
image_name=image_name, save_path='.')
We can use the full_screenshot() method to capture ful page screenshots.
from screenshot import ScreenShot
from datetime import datetime
if __name__ == '__main__':
screen = ScreenShot()
image_name = "{}.png".format(
datetime.now().strftime('%Y-%m-%d%H-%M-%S'))
screen.full_screenshot(url='https://python.org',
image_name=image_name)