How to use css selector path for selenium to get an attribute of an span class - python

816 views Asked by At

I would like to get the title attribute from the span class = g47SY lOXF2 but I can't find the correct css path.

Here is what I tried:

Number = self.browser.find_element_by_css_selector('ul li a span').get_attribute('title')

but it does not work.

Here is the HTML:

Here is the HTML

1

There are 1 answers

0
undetected Selenium On BEST ANSWER

To print the value of the title attribute i.e 251 you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "a[href$='followers/']>span[title]"))).get_attribute("title"))
    
  • Using XPATH:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[contains(@href, 'followers') and contains(., 'followers')]/span"))).get_attribute("title"))
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC