Keep looping for printing screenshots

Looking for some assistance. I have a Python script that loads URLs and takes screenshots.

Here's what I'm trying to accomplish:

  1. Instead of using an array, read URLs from a text file
  2. Take individual screenshots of each loaded URL and save them separately. Currently, the code is overwriting the screenshot.
from selenium import webdriver
from time import sleep

driver = webdriver.Firefox()
url_list = open("urls.txt", "r").readlines()
for url in url_list:
    driver.get(url.strip())
    driver.get_screenshot_as_file("{}_screenshot.png".format(url))
sleep(2)
driver.quit()
print("Finished.")

Answer №1

To store multiple URLs efficiently, save them in a text file and read each line one by one. Then capture a screenshot using the host name from the URL as part of the file name.

I have made adjustments to your code so that each URL's screenshot is saved separately. My implementation was done using Python 3.6.9.

Here is how my directory structure looks:

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

The content of links.txt is as follows:

http://facebook.com
http://apple.com

The contents of requirements.txt are:

selenium==3.141.0
urllib3==1.25.10

The contents of screenshots_of_links.py script are shown below:

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("All tasks completed successfully...")

Generated Output:

https://i.stack.imgur.com/QAmRt.png

https://i.stack.imgur.com/uwFiS.png

Details of Modifications Made:

  • Read the URLs stored in links.txt.
  • Trimmed any extra spaces from each line.
  • Used the hostname extracted from each URL as the filename for corresponding screenshot captures. The function urlparse(url).hostname was used for this purpose.

Further Reading:

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

pythonconcatenate strings to retrieve the value of the variable

men_clothing = ['shirts','jeans','trousers', 'jackets'] women_clothing = ['kurtas','jeans', 'sarees', 'tshirts'] men_shoes = ['sneakers','running', 'fl ...

Is there a way to determine if one key is contained within another key, and if so, merge the values of the two keys together?

Is it feasible to determine if a key is contained within another key in a dictionary, and if so, merge the values of the first key into the second key? For example: {'0': {'3', '1', '0'}, '3': {'3&apo ...

Having trouble displaying text within a span element after using execute_script with Selenium?

After numerous attempts to insert text into an input box that functions as a span element, I decided to seek guidance on the proper approach. In my quest, I turned to utilizing the execute_script() method, but disappointingly, no progress was made. Below ...

Finding the number of child elements using Selenium WebDriver with JavaScript

If my HTML looks like this <select class="list"> <option val="00"></option> <option val="01">One</option> </select> Although the JavaScript test file is running successfully, I am attempting to determine the number ...

Save the data to a document and ensure it is properly structured and

I am working with a set that contains pairs of names. Here's an example: names = {('nevio', 'chetan'), ('oishee', 'tafel'), ('utas', 'brea'), ('zoiie', 'fennell'), (&a ...

An error has arisen: ImportError unable to bring in the name 'imap' from 'itertools' (location unknown)

System Information: Operating System: Linux Mint 20 Ulyana Kernel Version: 5.4.0-65-generic Architecture: 64-bit Compiler: gcc v9.3.0 Desktop Environment: Cinnamon 4.6.7 Window Manager: muffin Display Manager: LightDM Distro Base: Ubuntu 20.04 focal I tho ...

Locate a Sub-Child href Element using Selenium

I am attempting to interact with a link using Selenium automation tool. <div id="RECORD_2" class="search-results-item"> <a hasautosubmit="true" oncontextmenu="javascript:return IsAllowedRightClick(this);" class="smallV110" href="#;cacheurl ...

What is the best way to retrieve information from both states and dictionaries simultaneously?

I have successfully obtained information from a json file for a specific state and district. However, I am now wondering how to retrieve data for all states and districts? Below is the code I have been using: def get_all_district_data(today): data = ...

Python Selenium problem: element state is invalid

Encountering an error message stating "invalid element state" while utilizing the Chrome driver for selenium. The task at hand involves inputting data into The initial problem arises when attempting to utilize the .click() method on the checkbox labeled ...

How can I create a loop to iterate through form/input files with varying titles?

Looping through input files with different titles Hello, I am trying to figure out how to loop through an input file with different titles. Each input file has a unique title, such as Safety Data Sheet, Certificate, etc. This is my current code: $titles ...

Having difficulty displaying retrieved data from sqlite in a tkinter entry widget

Having a bit of an issue here. The goal of my project is to sum up certain columns from the SQLite database and display the results in an entry box based on the checkboxes (representing months and years) selected by the user in the main GUI. I am able to g ...

Issue with Selenium Python: Element cannot be found - Is the website preventing access? (shadow DOM)

Struggling to automate form filling using Selenium in Python at the following URL: This is my current code snippet: from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_cond ...

Rephrase the entire document and insert it at a designated location within the file

Hey there, I'm currently developing a scoreboard program that can verify if a user's name exists in a text file. If the name is found, it updates the score on that line. Otherwise, it adds a new line with the user's name and score. def chec ...

Creating a MySQL database from a CSV file: A step-by-step guide

I am looking to streamline the database creation process by utilizing a CSV file that contains 160 columns and 15 rows of data. Manually assigning names for each column is proving to be quite challenging due to the large number of columns. I have managed t ...

Steps for sending a message to a server using WebSockets

My goal is to establish communication with a Server in order to receive responses. I attempted to utilize the official websocket APIs provided by the website, but I found them confusing or not functioning as desired. Therefore, I am exploring the option o ...

What is the most effective method for rounding numbers to 4 decimal places?

I'm struggling with rounding numbers to 4 decimal places in Python 3. Despite attempting to use the round function, I keep encountering a syntax error. fname = input("Enter a file name: ") try: ffile = open(fname) ssum = 0 nline = 0 f ...

Using a nested loop in Javascript to fetch JSON data

My goal is to display Categories and their corresponding subcategories in a specific order. However, my current method of using loops within loops is not producing the desired outcome: Category(Mobile) Category(Laptop) Subcategory(Iphone4) Subcategory(Iph ...

How can we most effectively test XML responses?

When it comes to testing webpages with xml responses, using Selenium IDE can be a challenge. While some opt for Selenium Remote Control or Pearl modules like WWW::Mechanize and Test::XML/Test::XPath, these may not be feasible options for those who prefer J ...

Difficulty sourcing a power supply

I've been facing some difficulties with referencing my outlet. Even though I managed to get the drag and drop feature working, thanks to a helpful member on stackoverflow, I still can't seem to make this outlet function properly. class controlle ...

Error: Unable to access the 'uname' attribute in the 'os' module within the dockerized environment (docker with Django REST framework)

Hello, I am working with Docker and DRF, and facing some challenges. Initially, I executed this command: pip install -r requirements.txt --use-deprecated=legacy-resolver Below are the contents of my requirements file: asgiref==3.5.2 backports.zoneinfo==0 ...