Jesper Ezra
Jesper Ezra

Reputation: 159

How to locate a specific image to get the src value with Selenium Python

I am trying to get the src of an image, however, it does not find the element of it does not filter to the image I need.

I have tried a couple of different xpaths and css selector including:

imgsrc = driver.find_element(By.CSS_SELECTOR, 'button[class="pv-top-card-profile-picture__image pv-top-card-profile-picture__image--show ember-view"]')

#imgsrc = driver.find_element(By.XPATH, "//img[@class='pv-top-card-profile-picture pv-top-card-profile-picture']")

That does not find the element.

imgsrc = driver.find_element(By.XPATH, "[//img[contains(@src, 'profile-displayphoto')]")

Is too broad and finds the incorrect image.

The html is as follows:

<div class="pv-top-card__non-self-photo-wrapper ml0">
  
  <button class="pv-top-card-profile-picture pv-top-card-profile-picture__container display-block
      pv-top-card__photo presence-entity__image EntityPhoto-circle-9
      " type="button">
      
      
    <img width="200" title="" src="https://media.licdn.com/dms/image/C4E03AQEW8FzkdjFD-w/profile-displayphoto-shrink_400_400/0/1516275678440?e=1683158400&amp;v=beta&amp;t=vmGZHtwkrttxt85prjTTySrnybP7L6lX858TpFgtWaQ" height="200" alt="" id="ember818" class="pv-top-card-profile-picture__image pv-top-card-profile-picture__image--show ember-view">

<!---->
<!---->
    

  </button>

  
<div class="presence-entity__indicator presence-entity__indicator--size-9 presence-indicator
    hidden
    presence-indicator--size-9">
  <span class="visually-hidden">
      Status is offline
      </span>
</div>
</div>

Upvotes: 1

Views: 1235

Answers (1)

undetected Selenium
undetected Selenium

Reputation: 193058

To print the value of the src attribute you can use either of the following locator strategies:

  • Using css_selector:

    print(driver.find_element(By.CSS_SELECTOR, "button.pv-top-card-profile-picture > img.pv-top-card-profile-picture__image.pv-top-card-profile-picture__image--show.ember-view").get_attribute("src"))
    
  • Using xpath:

    print(driver.find_element(By.XPATH, "//button[contains(@class, 'pv-top-card-profile-picture')]/img[@class='pv-top-card-profile-picture__image pv-top-card-profile-picture__image--show ember-view']"))
    
  • Note : You have to add the following imports :

    from selenium.webdriver.common.by import By
    

The desired element is a Ember.js enabled element, so ideally you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "a.name[title='Download']"))).get_attribute("value"))
    
  • Using XPATH:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[@class='name' and @title='Download']"))).get_attribute("value"))
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

You can find a relevant discussion in Python Selenium - get href value

Upvotes: 2

Related Questions