How to retrieve the title attribute through Selenium using Python?

The text of the title i.e. 3,862,378 isn’t within any <a> tag. Hence you can’t use find_element_by_partial_link_text(). Moreover the element is a dynamic element so so you have to induce WebDriverWait for the desired visibility_of_element_located() and you can use the following solution:

  • Using XPATH:

    print(WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[@href="https://stackoverflow.com/username/followers/" and contains(., 'followers')]/span"))).get_attribute("title"))
    
  • Using CSS_SELECTOR:

    print(WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "a[href="https://stackoverflow.com/username/followers/"]>span"))).get_attribute("title"))
    

Note: As per the documentation the actual method is get_attribute(name) but not get_attributes()

Leave a Comment