Home > Enterprise >  How to get attribute value using Selenium - Python
How to get attribute value using Selenium - Python

Time:04-21

HTML:

<div class data="#results">
    <a href="javascript:void(0);" title="testtest" >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)

CodePudding user response:

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
    
  • Related