2

Hi I need the src of image using XPATH in selenium

src.getAttribute("src")
img-src= driver.find_elements_by_xpath("//img[contains(@class,'_3me- _3mf1 img')]")
x=img-src.getAttribute("src")
print(x)

src of all images of a page

2 Answers 2

3

find_elements will return list so use find_element.

imgsrc= driver.find_element_by_xpath("//img[contains(@class,'_3me- _3mf1 img')]")
x=imgsrc.get_attribute("src")
print(x)

or if you want to use find_elements try this.

imgsrc= driver.find_elements_by_xpath("//img[contains(@class,'_3me- _3mf1 img')]")
for ele in imgsrc:
  x=ele.get_attribute("src")
  print(x)
Sign up to request clarification or add additional context in comments.

1 Comment

for doing that i just get first image of that page but they have many images and I need All it
1

From your code trials presumably you are trying to print the src attributes of the <img> elements having the class attribute as _3me-, _3mf1 and img. But the class attributes _3me- and _3mf1 are not static and are dynamically generated. So as a closest bet you can use either of the following Locator Strategies:

  • CSS_SELECTOR:

    print([ele.get_attribute("src") for ele in WebDriverWait(driver, 30).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "img.img")))])
    
  • XPATH:

    print([ele.get_attribute("src") for ele in WebDriverWait(driver, 30).until(EC.visibility_of_all_elements_located((By.XPATH, "//img[contains(@class, 'img')]")))])
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.