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

autotest_api.py

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()
    chrome_options.add_argument('--no-sandbox')
    chrome_options.add_argument('--window-size=1420,1080')
    chrome_options.add_argument('--headless')
    chrome_options.add_argument('--disable-gpu')
    driver = webdriver.Chrome(chrome_options=chrome_options)
  
    # The url to test
    url = f'https://reshetech.co.il/en/numbers?number={number}'
    
    driver.get(url)
  
    print(f"{url} from page {api_url}")

    time.sleep(15)
  
    #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) 
    time.sleep(2)
  
    dt = str(date.today())
    dt_obj = datetime.strptime(dt, "%Y-%m-%d").date()
    dt_str = dt_obj.strftime("%Y_%m_%d")
  
    driver.save_screenshot(f"{screenshots_dir}/number_{number}__{dt_str}.png")

    # end session and close the browser window
    driver.quit()
  
  
  if pyVal['next_page_url'] is not None:
    api_url = pyVal['next_page_url']
    test_pages(api_url)
    

test_pages(api_url)

 

Disclaimer

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.