如何使用硒的 css 选择器路径来获取范围 class - python 的属性

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

我想从跨度 class = g47SY lOXF2 中获取 title 属性,但找不到正确的 css 路径。

这是我尝试过的:

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

但它不起作用。

这里是 HTML:

要打印 title attribute 的值,即 251 你需要引入 for the visibility_of_element_located() and you can use either of the following :

  • 使用CSS_SELECTOR:

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

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[contains(@href, 'followers') and contains(., 'followers')]/span"))).get_attribute("title"))
    
  • 注意:您必须添加以下导入:

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