3

I have a question about selenium webdriver.

Everything is working fine but there is one element I don't get to work.

That's the html tag when i inspect the element:

<img src="https://s3.amazonaws.com/xxx/website/icons/rulesets/imgname.png" data-toggle="tooltip" data-placement="bottom" title="" data-original-title="White Animals">

CSS Selector is always the same but the "data-original-title" is always different. I want to look for the text after data-original-title. "White Animals" in this example.

try:
    element = WebDriverWait(driver, 1).until(expected_conditions.presence_of_element_located((By.CSS_SELECTOR, "CSSSELECTOR"), "White Animals"))
except:
    print("Not available")
else:
    print("found")

Can somebody tell me what i am missing?

The img src is also changing so it's maybe easier to parse the img src?

I hope somebody can help.

Guy
  • 46,488
  • 10
  • 44
  • 88
dernap65
  • 31
  • 2

2 Answers2

3

You can search by the data-original-title attribute

element = WebDriverWait(driver, 1).until(expected_conditions.presence_of_element_located((By.CSS_SELECTOR, '[data-original-title="White Animals"]')))
Guy
  • 46,488
  • 10
  • 44
  • 88
0

To print the attribute data-original-title you have to induce WebDriverWait for the desired 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, "img[src*='com/xxx/website/icons/rulesets/imgname'][data-toggle='tooltip'][data-placement='bottom']"))).get_attribute("data-original-title"))
    
  • Using XPATH:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//img[contains(@src, 'com/xxx/website/icons/rulesets/imgname') and @data-toggle='tooltip'][@data-placement='bottom']"))).get_attribute("data-original-title"))
    
  • 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 How to retrieve the title attribute through Selenium using Python?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352