'How to Extract Title value from selenium in Python?
I want to get title value from this through selenium but I am unable to do that. Need expert opinion.

items = driver.find_elements_by_tag_name("li")
for item in items:
followers = link.get_attribute('title')
print (followers)
it is showing me an empty string. Why is this so?
Solution 1:[1]
If the below xpath
//li//a[@href='/cristiano/followers']
is unique in HTML-DOM, then you can use the below code:
wait = WebDriverWait(driver, 20)
try:
a_tag = wait.until(EC.visibility_of_element_located((By.XPATH, "//li//a[@href='/cristiano/followers']")))
print(a_tag.find_element(By.XPATH, ".//descendant::span").get_attribute('title'))
except:
print("something went wrong")
pass
You do not really need a loop for this use case.
Imports:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Steps to check if element is unique or not:
Press F12 in Chrome -> go to element section -> do a CTRL + F -> then paste the xpath and see, if your desired element is getting highlighted with 1/1 matching node.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | cruisepandey |
