Last modified: Nov 28, 2023 By Alexander Williams
Python Selenium: Get Image Src
Table Of Contents
Example 1: Get Image Src by Tag Name
from selenium import webdriver
# Create a Chrome driver
driver = webdriver.Chrome()
# Navigate to a webpage with an image
driver.get('https://example.com')
# Find the image element by tag name
image_element = driver.find_element_by_tag_name('img')
# Get the source attribute
image_src = image_element.get_attribute('src')
# Print the image source
print(f"Image Src: {image_src}")
# Close the browser
driver.quit()
Output:
# Image Src: [URL of the image]
Example 2: Get Multiple Image Src by Tag Name
from selenium import webdriver
# Create a Chrome driver
driver = webdriver.Chrome()
# Navigate to a webpage with multiple images
driver.get('https://example.com')
# Find all image elements by tag name
image_elements = driver.find_elements_by_tag_name('img')
# Get and print the source attribute for each image
for idx, image_element in enumerate(image_elements, start=1):
image_src = image_element.get_attribute('src')
print(f"Image {idx} Src: {image_src}")
# Close the browser
driver.quit()
Output:
# Image 1 Src: [URL of the first image]
# Image 2 Src: [URL of the second image]
# ...
Example 3: Get Image Src by XPath
from selenium import webdriver
# Create a Chrome driver
driver = webdriver.Chrome()
# Navigate to a webpage with an image
driver.get('https://example.com')
# Find the image element by XPath
image_element = driver.find_element_by_xpath('//img[@class="example-image"]')
# Get the source attribute
image_src = image_element.get_attribute('src')
# Print the image source
print(f"Image Src: {image_src}")
# Close the browser
driver.quit()
Output:
# Image Src: [URL of the image]
Example 4: Get Image Src by CSS Selector
from selenium import webdriver
# Create a Chrome driver
driver = webdriver.Chrome()
# Navigate to a webpage with an image
driver.get('https://example.com')
# Find the image element by CSS selector
image_element = driver.find_element_by_css_selector('img.example-image')
# Get the source attribute
image_src = image_element.get_attribute('src')
# Print the image source
print(f"Image Src: {image_src}")
# Close the browser
driver.quit()
Output:
# Image Src: [URL of the image]
Example 5: Get Image Src with Attribute Value Containing Specific Text
from selenium import webdriver
# Create a Chrome driver
driver = webdriver.Chrome()
# Navigate to a webpage with an image
driver.get('https://example.com')
# Find the image element with a specific attribute value
attribute_value = 'logo'
image_element = driver.find_element_by_xpath(f'//img[contains(@src, "{attribute_value}")]')
# Get the source attribute
image_src = image_element.get_attribute('src')
# Print the image source
print(f"Image Src: {image_src}")
# Close the browser
driver.quit()
Output:
# Image Src: [URL of the image]
Example 6: Handle Missing Src Attribute
from selenium import webdriver
# Create a Chrome driver
driver = webdriver.Chrome()
# Navigate to a webpage with an image without src attribute
driver.get('https://example.com')
# Find the image element
image_element = driver.find_element_by_tag_name('img')
# Get the source attribute or handle missing attribute
image_src = image_element.get_attribute('src')
if image_src:
print(f"Image Src: {image_src}")
else:
print("Image has no source attribute.")
# Close the browser
driver.quit()
Output:
# Image has no source attribute.