简体   繁体   English

无法在 Selenium Python 中获取元素的文本

[英]Can't get the text of an element in Selenium Python

I am trying to get the text of an element and when I run it, it gives me the text for some of the elements and some it doesn't.我正在尝试获取元素的文本,当我运行它时,它会为我提供一些元素的文本,而有些则没有。 I can't figure out why this is happening?我不明白为什么会这样?

                # 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)

You have to use one of the following您必须使用以下其中一项

types[i].get_attribute('innerHTML')
types[i].get_attribute('innerText')
types[i].get_attribute('textContent')

see differences here在这里查看差异

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM