Giter VIP home page Giter VIP logo

screenshot_selenium_python's Introduction

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)

screenshot_selenium_python's People

Contributors

mostafataheri avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.