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

QHarr
  • 83,427
  • 12
  • 54
  • 101
Yassar Farooq
  • 51
  • 1
  • 2
  • 7

2 Answers2

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)
KunduK
  • 32,888
  • 5
  • 17
  • 41
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')]")))])
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352