Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Schedule Screenshots of Multiple URLs from a CSV

· 5 min read · Home Automation

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

This guide shows you how to automate taking screenshots of many web pages listed in a CSV file, on a regular schedule. You’ll set up a script that reads URLs from a CSV, captures full-page screenshots, saves them with clear filenames, and runs automatically using a task scheduler. No coding experience is needed beyond following the steps, and you’ll use free, widely available tools.

What You'll Need

  • A computer running Windows, macOS, or Linux.
  • Python 3.7 or newer installed. Download from python.org if needed.
  • Google Chrome or Chromium browser installed.
  • A CSV file with a column named url (or you can adjust the script).
  • Basic familiarity with your operating system’s command line (Terminal on macOS/Linux, Command Prompt or PowerShell on Windows).

Step-by-Step

  1. Prepare your CSV file. Open a text editor or spreadsheet program. Create a file with a header row. At minimum, include a column named url. You can also add a name column for friendly filenames. Save it as urls.csv in a dedicated folder, e.g., screenshot-scheduler.
  2. Install required Python packages. Open your command line and navigate to your project folder. Run: pip install selenium pandas. This installs the Selenium browser automation library and pandas for CSV handling.
  3. Download the ChromeDriver. Go to the ChromeDriver download page (sites.google.com/chromium.org/driver) and download the version that matches your Chrome browser version. Place the chromedriver executable in your project folder. On Windows, it’s chromedriver.exe.
  4. Write the screenshot script. Create a new file named screenshot.py in the same folder. Copy and paste the following code:
    import pandas as pd
    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    import os
    import time
    
    def take_screenshots(csv_file, output_dir='screenshots'):
        # Read CSV
        df = pd.read_csv(csv_file)
        if 'url' not in df.columns:
            raise ValueError("CSV must have a 'url' column")
        
        # Create output directory
        os.makedirs(output_dir, exist_ok=True)
        
        # Configure Chrome to run headless (no GUI)
        chrome_options = Options()
        chrome_options.add_argument('--headless')
        chrome_options.add_argument('--window-size=1920,1080')
        
        driver = webdriver.Chrome(executable_path='./chromedriver', options=chrome_options)
        
        for index, row in df.iterrows():
            url = row['url']
            # Use 'name' column if available, else use index
            if 'name' in df.columns and pd.notna(row.get('name')):
                name = str(row['name'])
            else:
                name = f"screenshot_{index}"
            
            # Clean filename
            name = name.replace(' ', '_').replace('/', '_')
            
            try:
                driver.get(url)
                time.sleep(2)  # Wait for page to load
                # Take full-page screenshot
                height = driver.execute_script("return document.body.scrollHeight")
                driver.set_window_size(1920, height)
                time.sleep(1)
                driver.save_screenshot(f"{output_dir}/{name}.png")
                print(f"Saved: {name}.png")
            except Exception as e:
                print(f"Error on {url}: {e}")
        
        driver.quit()
    
    if __name__ == '__main__':
        take_screenshots('urls.csv')
        
  5. Test the script manually. In the command line, run python screenshot.py. Check the screenshots folder for PNG files. If you get errors, see Troubleshooting below.
  6. Set up a scheduled task. Now you’ll make the script run automatically.
    • Windows: Open Task Scheduler. Create a Basic Task. Set the trigger (e.g., daily at 9 AM). Set the action to “Start a program”. In the Program/script field, enter the full path to your Python executable (e.g., C:\Python39\python.exe). In Arguments, enter screenshot.py. In Start in, enter the full path to your project folder. Finish.
    • macOS: Use crontab. Open Terminal and run crontab -e. Add a line like 0 9 * * * cd /path/to/project && /usr/bin/python3 screenshot.py. Save and exit.
    • Linux: Same as macOS using cron. Or use systemd timers if you prefer.
  7. Verify the schedule works. After setting up, wait for the next scheduled run or trigger it manually to confirm. Check that new screenshots appear in the folder.

Common Mistakes

  • ChromeDriver version mismatch: If Chrome updates, the driver may stop working. Always download a matching version.
  • Incorrect file paths: Using relative paths in the script can cause issues when run from a scheduler. Use absolute paths in the scheduled task.
  • CSV formatting: Ensure the CSV uses commas as delimiters. If you have commas in URLs, they need to be quoted.
  • Not waiting for page load: Some pages load slowly. The fixed 2-second wait may be too short. Increase it if needed.

Tips

  • Add a delay column to your CSV to set per-URL wait times.
  • Use a unique name column to avoid overwriting screenshots.
  • For very long pages, consider using a library like selenium-screen-shot that handles full-page capture better.
  • Run the script once manually before scheduling to ensure the environment is set up correctly.
  • If you need to capture pages that require login, you can add cookies or use a persistent profile.

Troubleshooting

  • “chromedriver” executable needs to be in PATH: Either place the driver in a directory listed in PATH, or modify the script to use the full path.
  • “ModuleNotFoundError: No module named 'selenium'”: You forgot to install Selenium. Run pip install selenium again.
  • Chrome crashes or doesn’t start: Add --no-sandbox to the Chrome options if you’re on Linux or in a restricted environment.
  • Screenshots are blank or partial: The page might not have loaded fully. Increase the time.sleep() or use WebDriverWait to wait for a specific element.
  • Task Scheduler runs but no screenshots: Check the “Start in” field. It must be the project folder, otherwise Python can’t find urls.csv.
  • Permission errors on macOS/Linux: Ensure the script has execute permissions (chmod +x screenshot.py) and that the output folder is writable.

Frequently Asked Questions

Can I use Firefox instead of Chrome?

Yes, you can. Replace webdriver.Chrome with webdriver.Firefox and use geckodriver instead of ChromeDriver. The rest of the script remains the same.

How do I capture full-page screenshots for mobile view?

Set the window size to a mobile resolution, e.g., --window-size=375,812, and add a mobile user agent. You can also use Chrome DevTools Protocol for device emulation.

What if my CSV has URLs with special characters?

Pandas handles most special characters, but ensure the CSV is encoded in UTF-8. If you see errors, open the CSV in a text editor and re-save as UTF-8.

Can I schedule the script to run every hour?

Yes, adjust the trigger in Task Scheduler or the cron expression. For example, 0 * * * * runs every hour at minute 0.

How do I avoid duplicate screenshots?

Include a timestamp in the filename. Modify the script to append the current date and time using Python’s datetime module.

Is there a way to run this without installing Python?

You could use a browser extension or an online service, but those are limited. Python is the most flexible and free approach.