1

HTML:

<div class data="#results">
    <a href="javascript:void(0);" title="testtest" class="btn btn-white btn-sm btn-rounded dropdown-toggle">results</a>

How do I extract the value of title using selenium?

This is what I have tried so far but it gives None.

driver.get(url)
toogle = driver.find_element_by_xpath("XPATH_HERE")
val = toogle.get_attribute("title")
print(val)
2
  • Are you sure there are two title attributes? Commented Apr 20, 2022 at 21:43
  • Oh no, sorry there's only one title attribute. Commented Apr 20, 2022 at 21:45

1 Answer 1

2

To print the value of the title attribute you have to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR and class attribute:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div[data='#results'] > a.btn.btn-white.btn-sm.btn-rounded.dropdown-toggle"))).get_attribute("title"))
    
  • Using XPATH and innerText:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@data='#results']/a[text()='results']"))).get_attribute("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
    
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the help :)

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.