可以在Selenium Python中获取元素的文本

发布于 2025-02-01 11:49:40 字数 1017 浏览 4 评论 0原文

我正在尝试获取一个元素的文本,当我运行它时,它为我提供了一些元素的文字,其中一些元素没有。我不知道为什么会发生这种情况?

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

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

熊抱啵儿 2025-02-08 11:49:40

您必须使用以下一个

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

查看差异在这里

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文