0

I'm trying to select this element with Python Selenium, but don't know how since it does not contain and id/class name/etc..

Here is the HTML:

<img src="/resource/1599091587000/nforce__SLDS0102/assets/icons/utility/chevrondown_60.png">

I've tried:

chrome_browser.find_element_by_xpath("//img[contains(text(),'/resource/1599091587000/nforce__SLDS0102/assets/icons/utility/chevrondown_60.png')]")
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Helper
  • 21
  • 3
  • target src attribute... something like: chrome_browser.find_element_by_xpath("//img[contains(@src,'chevrondown_60')]") – pcalkins Nov 30 '20 at 20:08
  • 1
    either share the url or try yourself to open the webdeveloper tool (in firefox press F12) and then copy either the css selector or the xpath, that should work – Timeler Nov 30 '20 at 20:16
  • You can try to locate the element using surrounding elements (for example its parent). What is the surrounding HTML? It may actually be more reliable work than relying on src content. – Ondrej Machulda Nov 30 '20 at 20:18

2 Answers2

0

Since text() searches for the inner text for the HTML tag, you should not use that to search for this particular element since the URL is in the src attribute.

You may search it using the src attribute

//img[@src='/resource/1599091587000/nforce__SLDS0102/assets/icons/utility/chevrondown_60.png')]

or just a part of it using

//img[contains(@src,'chevrondown_60')]

kennysliding
  • 2,783
  • 1
  • 10
  • 31
0

The element seems to be a dynamic element. So to locate the element you can use either of the following Locator Strategies:

  • Using css_selector:

    element = chrome_browser.find_element_by_css_selector("img[src^='/resource'][src$='assets/icons/utility/chevrondown_60.png']")
    
  • Using xpath:

    element = chrome_browser.find_element_by_xpath("//img[starts-with(@src, '/resource') and contains(@src, 'assets/icons/utility/chevrondown_60.png')]")
    

Ideally, to locate the element 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(chrome_browser, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "img[src^='/resource'][src$='assets/icons/utility/chevrondown_60.png']")))
    
  • Using XPATH:

    element = WebDriverWait(chrome_browser, 20).until(EC.visibility_of_element_located((By.XPATH, "//img[starts-with(@src, '/resource') and contains(@src, 'assets/icons/utility/chevrondown_60.png')]")))
    
  • 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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352