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.

1
  • Can you provide the URL of the webpage on which you are trying? Commented Sep 10, 2019 at 7:24

2 Answers 2

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"]')))
Sign up to request clarification or add additional context in comments.

Comments

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.