[英]How to extract text from time tag with Selenium (Python)
I tried to extract all text from a 'time' tag.我试图从“时间”标签中提取所有文本。 The HTML is from this page : https://www.python.org
HTML 来自此页面: https : //www.python.org
Here is my code:这是我的代码:
event_times = driver.find_elements(By.CSS_SELECTOR, value=".event-widget time")
for time in event_times:
print(time.get_attribute("innerHTML"))
I have this output:我有这个输出:
<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
If I change to :如果我改为:
for time in event_times:
print(time.text)
The output:输出:
11-13
11-15
11-18
11-19
11-24
My question : is there a direct way to display all the text contained in the time tag, namely 2021-11-13
, 2021-11-15
, etc...?我的问题:有没有一种直接的方法来显示时间标签中包含的所有文本,即
2021-11-13
、 2021-11-15
等......?
Except to separate into two searches (in span for "year" and in time for "month-day"), I don't see how to do this...除了分成两个搜索(跨度为“年”和时间为“月-日”),我不知道如何做到这一点......
To extract all the texts from the <time>
tags eg 2021-11-05 , using Selenium and python you have to induce WebDriverWait for visibility_of_all_elements_located()
and you can use either of the following Locator Strategies :要从
<time>
标签中提取所有文本,例如2021-11-05 ,使用Selenium和python,您必须为visibility_of_all_elements_located()
引入WebDriverWait ,您可以使用以下任一定位器策略:
Using CSS_SELECTOR
:使用
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")))])
Using XPATH
:使用
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")))])
Console Output:控制台输出:
['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']
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.