homepage >> Take screenshots with Python selenium    

Yossef Benharosh is an apt web developer and the author of the eBook The essentials of object oriented PHP.

Yossef Benharosh web developer profile linkedin twitter github

Take screenshots with Python selenium

We use Selenium Python automation suite for site testing because it controls the browser and allows us to direct its behavior. For example, selenium can be used to test the behavior of a form in different scenarios and in different browsers and then run the script on an automaton every day at the same time to make sure the form works.

The following script gets a list of links from an api then uses headless chrome to crawl each link and take a screenshot of each webpage.

Here I use Google Chrome browser in headless form (without any GUI).


from selenium import webdriver
import time
import json, requests
from datetime import date, datetime

from selenium.webdriver.chrome.options import Options

# screenshots directory
screenshots_dir = '/home/joe/Pictures/test_api'

# get the data from the api
api_url = 'https://reshetech.co.il/en/simple_api'

screen_width = 1920

def test_pages(api_url):

  #params = {'page': 4}
  response = requests.get(api_url)
  pyVal = json.loads(response.text)
  result = pyVal['data']

  # print each row inside a loop
  for row in result:
    number = row['number']
    # create a new Chrome session
    # For other options:
    # https://peter.sh/experiments/chromium-command-line-switches/
    chrome_options = webdriver.ChromeOptions()
    driver = webdriver.Chrome(chrome_options=chrome_options)
    # The url to test
    url = f'https://reshetech.co.il/en/numbers?number={number}'
    print(f"{url} from page {api_url}")

    #the element with longest height on page
    ele=driver.find_element("xpath", '//div[@class="main"]')
    total_height = ele.size["height"]+500

    driver.set_window_size(screen_width, total_height) 
    dt = str(date.today())
    dt_obj = datetime.strptime(dt, "%Y-%m-%d").date()
    dt_str = dt_obj.strftime("%Y_%m_%d")

    # end session and close the browser window
  if pyVal['next_page_url'] is not None:
    api_url = pyVal['next_page_url']




As far as I know, the script within this page is functional and harmless. However I make no guarantees. I also take no responsibility for any damage you may do when using the script (the chances of any problems occuring are slim but who knows). Please use this script responsibly and judgementally.