Home > Mobile >  How to Extract Title value from selenium in Python?
How to Extract Title value from selenium in Python?

Time:05-12

I want to get title value from this through selenium but I am unable to do that. Need expert opinion.

1

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?

CodePudding user response:

Try to use item.get_attribute('title') instead of link.get_attribute('title')

items = driver.find_elements_by_tag_name("li")
for item in items:
    followers = item.get_attribute('title')
    print (followers)

CodePudding user response:

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.

  • Related