Search code examples
pythonpython-3.xseleniumloopsscreenshot

Loop for print screen


Need some help. I got this python code to load url and take a screen print.

I need to achieve this:

  1. instead of array, read url from text file
  2. take screen print of each url that is loaded and save it. with the code the screen print is over written
from selenium import webdriver
from time import sleep

driver = webdriver.Firefox()
url = ["http://google.com", "http://cisco.com"]
for element in url:
    driver.get(element)
    driver.get_screenshot_as_file("screenshot.png")
sleep(2)
driver.quit()
print("end...")

Solution

  • Store the URLs in a text file and then read line by line. Then take screenshot using file name with the host name of the URL.

    I have modified your code and could store the screenshot of each url in separate file. I have used Python 3.6.9.

    Directory structure:

    .
    ├── links.txt
    ├── requirements.txt
    └── screenshots_of_links.py
    

    links.txt:

    http://google.com
    http://cisco.com
    

    requirements.txt:

    selenium==3.141.0
    urllib3==1.25.10
    

    screenshots_of_links.py:

    from selenium import webdriver
    from urllib.parse import urlparse
    from time import sleep
    
    
    driver = webdriver.Firefox()
    
    with open("links.txt") as url_file:
        for line in url_file.readlines():
            url = line.strip()
            if url != "":        
                driver.get(url)
                host = urlparse(url).hostname
                driver.get_screenshot_as_file("{}.png".format(host))            
                sleep(2)
    
    driver.quit()
    print("end...")
    

    Output:

    cisco.com

    google.com

    Modification details:

    • Read the URLs from links.txt text file.
    • Trim each line of the file.
    • Parsed each URL and used the hostname as filename of screenshot. urlparse(url).hostname returns the hostname of a valid URL.

    Reference: