2

This is my code : print(browser.find_element_by_partial_link_text("followers").get_attributes("title")). I am using find_element_by_partial_link_text because maybe the xpath could be unique for each user. But my code returns nothing. This is the element's adress:

<li class=" LH36I">
  <a class=" _81NM2" href="/username/followers/">
    <span class="g47SY lOXF2" title="3,862,378">3.8m</span> 
      " followers"
  </a>
</li>

So my question is how can i get the value in the title using find_element_by_partial_link_text ? Thanks in advance.

SOLVED : You can look at DebanjanB 's answer.

  • 1
    Looks like you want to extract the title of the span element, but you will find and catch the link element. Try to look into xpaths, first looking for the link containing "followers", then going one step down to the span, extracting its title. – nostradamus Aug 29 '19 at 11:06

3 Answers3

4

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='/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='/username/followers/']>span"))).get_attribute("title"))
    

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

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

Use following xpath to get the value.

print(browser.find_element_by_xpath("//a[contains(.,'followers')]/span").get_attribute("title"))
KunduK
  • 32,888
  • 5
  • 17
  • 41
  • 1
    @DebanjanB : Just copy and paste OP code.Thanks for that.Nowadays so busy in work not able to contribute :-D – KunduK Aug 29 '19 at 11:20
1

Instead you can use xpath like this:

print(browser.find_element_by_xpath("//li[contains(@class,'LH36I')]//a[contains(@class,'_81NM2')]//span[contains(@class,'g47SY lOXF2')]").get_attributes("title"))
frianH
  • 7,295
  • 6
  • 20
  • 45