1

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

QHarr
  • 72,711
  • 10
  • 44
  • 81
Yassar Farooq
  • 19
  • 2
  • 7

2 Answers2

1

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)
KunduK
  • 26,790
  • 2
  • 10
  • 32
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')]")))])
    
DebanjanB
  • 118,661
  • 30
  • 168
  • 217