Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Using Selenium python, finding an element by a known src link

I am trying to find an element by using Selenium with python, by a known link written in src. The element is the point picture on the left of the address of a location in Google Maps.

Namely the html for the element I’m trying to select:

<img alt="" jstcache="935" src="//www.gstatic.com/images/icons/material/system_gm/1x/place_gm_blue_24dp.png" class="Liguzb" jsan="7.Liguzb,0.alt,8.src">

How can I select the given element by searching for it by using the link:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

www.gstatic.com/images/icons/material/system_gm/1x/place_gm_blue_24dp.png

Thanks.

>Solution :

To locate the element as the value of src attribute is know to you, you can use either of the following Locator Strategies:

  • Using css_selector:

    element = driver.find_element(By.CSS_SELECTOR, "img.Liguzb[src*='gstatic.com/images/icons/material/system_gm/1x/place_gm_blue_24dp']")
    
  • Using xpath:

    element = driver.find_element(By.XPATH, "//img[@class='Liguzb' and contains(@src, 'gstatic.com/images/icons/material/system_gm/1x/place_gm_blue_24dp')]")
    

To locate a visible element instead of presence_of_element_located() you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "img.Liguzb[src*='gstatic.com/images/icons/material/system_gm/1x/place_gm_blue_24dp']")))
    
  • Using XPATH:

    element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//img[@class='Liguzb' and contains(@src, 'gstatic.com/images/icons/material/system_gm/1x/place_gm_blue_24dp')]")))
    
  • 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
    
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading