Skip to content

In this Python Selenium screenshot tutorial, we are going to explore different ways of taking screenshots using Selenium’s Python bindings.

Notifications You must be signed in to change notification settings

MostafaTaheri/screenshot_selenium_python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Take A Screenshot Using Python & Selenium

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 :

Chrome

Firefox

Edge

Internet Explorer


Code Walkthrough

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.


Requirements

selenium

PyYAML

lazy-load


Capturing Python Selenium Screenshots Of A Particular Element

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='.')

Capturing full page screenshots

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)

About

In this Python Selenium screenshot tutorial, we are going to explore different ways of taking screenshots using Selenium’s Python bindings.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages