How to get all images in Selenium Python?

Selenium Python – Get all images in a webpage

In this tutorial, you shall learn how to get all the images in a webpage, using Selenium in Python language.

To get all the images in a webpage in Selenium Python, you can use find_elements() function with By.TAG_NAME as selector and “img” for the tag name.

driver.find_elements(By.TAG_NAME, 'img')

This function call returns all the images in the webpage. You may iterate over these images using a For loop.

Examples

In the following examples, we shall consider loading the HTML file at path https://pythonexamples.org/tmp/selenium/index-30.html . The contents of this HTML file is given below.

<html>
<body> 
    <h3>Hello World</h3> 
    <img id="image1" src="https://pythonexamples.org/wp-content/uploads/2023/07/pe4-2.png" alt="My sample image 1">
    <img id="image2" src="https://pythonexamples.org/wp-content/uploads/2023/06/If.png" alt="My sample image 2">
    <img id="image3" src="https://pythonexamples.org/wp-content/uploads/2023/04/test_image-1.jpg" alt="My sample image 3" hidden>
</body>
</html>
Copy

1. Get all the images in the webpage

In the following program, we initialize a driver object, load the specified URL, get all the images in webpage, and print the count of the images to output.

Since the webpage has three images, we should get the output accordingly.

Python Program

from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By

# Setup chrome driver
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))

# Navigate to the url
driver.get('https://pythonexamples.org/tmp/selenium/index-30.html')

# Find all images
images = driver.find_elements(By.TAG_NAME, 'img')

# Print number of images
print(f'There are {len(images)} images.')

# Close the driver
driver.quit()
Copy

Output

There are 3 images.

2. Iterate over all the images

You may use a For loop to iterate over the images, and access each of them.

For example, let us iterate over the images, and print the source URL of each image.

Python Program

from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By

# Setup chrome driver
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))

# Navigate to the url
driver.get('https://pythonexamples.org/tmp/selenium/index-30.html')

# Find all images
images = driver.find_elements(By.TAG_NAME, 'img')

# Print source URL of images
for image in images:
    print(image.get_attribute('src'))

# Close the driver
driver.quit()
Copy

Output

https://pythonexamples.org/wp-content/uploads/2023/07/pe4-2.png
https://pythonexamples.org/wp-content/uploads/2023/06/If.png
https://pythonexamples.org/wp-content/uploads/2023/04/test_image-1.jpg

Summary

In this Python Selenium tutorial, we have seen how to get all the images in a webpage, with example programs.

Quiz on Selenium

Q1. What is Selenium Python used for?

Not answered

Q2. What is a web driver in Selenium Python?

Not answered

Q3. What is the method used to switch to a different frame in Selenium Python?

Not answered

Q4. Which of the following is not a commonly used web element locator in Selenium Python?

Not answered

Q5. What is the method used to select a value from a drop-down list in Selenium Python?

Not answered
Code copied to clipboard successfully 👍