如何使用 Selenium 从时间标签中提取文本 (Python)

How to extract text from time tag with Selenium (Python)

我试图从 'time' 标签中提取所有文本。 HTML 来自此页面:https://www.python.org

这是我的代码:

event_times = driver.find_elements(By.CSS_SELECTOR, value=".event-widget time")
    
for time in event_times:
    print(time.get_attribute("innerHTML"))

我有这个输出:

<span class="say-no-more">2021-</span>11-13
<span class="say-no-more">2021-</span>11-15
<span class="say-no-more">2021-</span>11-18
<span class="say-no-more">2021-</span>11-19
<span class="say-no-more">2021-</span>11-24

如果我改成:

for time in event_times:    
    print(time.text)

输出:

11-13
11-15
11-18
11-19
11-24

我的问题:有没有直接显示时间标签中包含的所有文本的方法,即 2021-11-132021-11-15 等...?

除了分成两个搜索(跨度为“年”和时间为“月-日”),我不知道该怎么做...

<time> 标签中提取所有文本,例如2021-11-05, 使用 and you have to induce for visibility_of_all_elements_located() and you can use either of the following :

  • 使用CSS_SELECTOR:

    print([my_elem.text for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "ul.menu>li time")))])
    
  • 使用XPATH:

    print([my_elem.text for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//ul[@class='menu']/li//time")))])
    
  • 控制台输出:

    ['2021-11-05', '2021-11-02', '2021-10-26', '2021-10-19', '2021-10-18', '2021-11-13', '2021-11-15', '2021-11-18', '2021-11-19', '2021-11-24']
    
  • 注意:您必须添加以下导入:

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