无法获取 Selenium 中元素的文本 Python
Can't get the text of an element in Selenium Python
我正在尝试获取元素的文本,当我 运行 时,它为我提供了一些元素的文本,而另一些则没有。我不明白为什么会这样?
# Type and Tags
details_info = []
types = WebDriverWait(driver, 10).until(
EC.presence_of_all_elements_located((By.TAG_NAME, "span")))
for i in range(0, 19):
if types[i].text:
if types[i].text in reference:
details_info.append(types[i].text)
# Types
type_info = []
for d in details_info:
if type(d) is str:
if d in type_legend:
type_info.append(d)
info.append(type_info)
# Tags
tag_info = []
for t in details_info:
if type(t) is str:
if t in tag_legend:
tag_info.append(t)
info.append(tag_info)
您必须使用以下其中一项
types[i].get_attribute('innerHTML')
types[i].get_attribute('innerText')
types[i].get_attribute('textContent')
查看差异here
我正在尝试获取元素的文本,当我 运行 时,它为我提供了一些元素的文本,而另一些则没有。我不明白为什么会这样?
# Type and Tags
details_info = []
types = WebDriverWait(driver, 10).until(
EC.presence_of_all_elements_located((By.TAG_NAME, "span")))
for i in range(0, 19):
if types[i].text:
if types[i].text in reference:
details_info.append(types[i].text)
# Types
type_info = []
for d in details_info:
if type(d) is str:
if d in type_legend:
type_info.append(d)
info.append(type_info)
# Tags
tag_info = []
for t in details_info:
if type(t) is str:
if t in tag_legend:
tag_info.append(t)
info.append(tag_info)
您必须使用以下其中一项
types[i].get_attribute('innerHTML')
types[i].get_attribute('innerText')
types[i].get_attribute('textContent')
查看差异here