Unable to get image src attribute in python using selenium

Question:

When I try to get the src it returns None. The code should locate the image by its class name and get the attribute.

import selenium.webdriver as webdriver
import time
from selenium.webdriver.common.by import By

driver = webdriver.Firefox()

url = ('https://www.instagram.com/cats')
driver.get(url)

time.sleep(3)

imgs = driver.find_element(By.CLASS_NAME, "_aagv").get_attribute("src")
print(imgs)

driver.quit()

enter image description here

I tired to use it with for loop as well but the results were same None. Any suggestions how to get it working?

Asked By: uzdisral

||

Answers:

The div class has the class that you are searching for ("_aagv"). You need to get the inner image element (<img>), because it is the element that has the src tag on it.

You could either change your selector to match the Image tag directly, or you could select Image tags inside of the <div> tag you matched.

Answered By: Xiddoc

You can get the value of the src attribute using the below locator:

time.sleep(3)
images = driver.find_elements(By.CSS_SELECTOR, "._aagv img")

for image in images:
    print(image.get_attribute("src"))
Answered By: AbiSaran
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.