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
  • 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. Commented Aug 29, 2019 at 11:06

3 Answers 3

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()

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry :) I am new here. Thanks again.
2

Use following xpath to get the value.

print(browser.find_element_by_xpath("//a[contains(.,'followers')]/span").get_attribute("title"))

1 Comment

@DebanjanB : Just copy and paste OP code.Thanks for that.Nowadays so busy in work not able to contribute :-D
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"))

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.