【问题标题】:How to get text value of elements in Selenium Python?如何在 Selenium Python 中获取元素的文本值?
【发布时间】:2021-05-28 13:45:31
【问题描述】:

这是我第一次使用 Selenium。我正在尝试获取这两个元素的文本值: element1

element 2

我通过 h3 标签获得了第一个元素。现在,这两个元素都没有出现。我无法通过任何使用 ID 的东西找到它,因为它变化很大。

有什么帮助吗?

更新@Chikabala:这是 .text 在 print 语句中时的输出。

Traceback (most recent call last):
  File "C:\Users\re123el\Py5123s\create\try.py", line 42, in <module>
    print(elementList.text)
AttributeError: 'list' object has no attribute 'text'

这是将 .text 添加到 xpath 末尾时的输出:

vscGnRMlqsD8rQ
Traceback (most recent call last):
  File "C:\Users\rebel\PycharmProjects\create\try.py", line 40, in <module>
    elementList = sheto.find_elements_by_tag_name("td")
AttributeError: 'str' object has no attribute 'find_elements_by_tag_name'

更新 #2: 这是原始代码:

keto = browser.find_elements_by_tag_name("h3")[1].text
print(keto)
sheto = browser.find_element_by_xpath("//table[@class='preftable']/tbody/tr/td[@class='prefright']").text
print(sheto)

这是原始输出:

C:\Users\rebel\PycharmProjects\create\venv\Scripts\python.exe C:/Users/rebel/PycharmProjects/create/try.py
GWrS4IyFkO-bfw


Process finished with exit code 0

域名是:https://www.reddit.com/prefs/apps 状态:已登录、已创建应用、脚本 需要的元素:app id(h3 标签)和秘密(秘密旁边的 api,在 td 标签中找到)

【问题讨论】:

  • 您遇到的错误是什么?另外,您在 .text 之前的 sheto 变量中缺少“)”吗?是不是笔误?
  • 刷新页面有助于通过 h3 标签获取第一个元素。我没有得到任何代码,但是:sheto = browser.find_elements_by_xpath("//table[@class='preftable']/tbody/tr/td[@class='prefright']")[1]。文本打印(sheto)
  • 可以分享一下html吗?您的屏幕截图没有显示太多调试信息
  • 我现在没有收到任何错误。第一个元素是成功的。第二个没有出现。我尝试将 [1] 更改为 [0] 并没有任何变化。
  • 好的,等一下

标签: python selenium


【解决方案1】:

假设您需要来自第一个 tr 标签的文本。 试试这个 xpath:

driver.find_element_by_xpath(".//table[@class='preftable']/tbody/tr[1]/td[@class='prefright']").text

更新:

driver.find_element_by_xpath(".//table[@class='preftable']/tbody/tr/th[text()='secret']//following::td[1]").text

【讨论】:

  • 试过了,仍然没有得到第二个元素的任何输出。你能看看reddit.com/prefs/apps - 登录并创建应用程序,脚本
  • 第二个元素是什么?
  • 应用提供的“秘密”
  • 绝对xpath:/html/body/div[3]/div[2]/ul/li/div[4]/div[1]/form/table/tbody/tr[1] /td
  • 更新了我的代码。请检查并告诉我它是否工作
【解决方案2】:

您可以像这样使用find_elements_by_

sheto = browser.find_element_by_xpath(".//table[@class='preftable']/tbody/tr[1]/td[@class='prefright']")

elementList = sheto.find_elements_by_tag_name("td")

print(elementList.text )

sheto 包含第一个元素,elementList 包含它的子元素

如果 sheto 包含超过 1 个您可以使用的元素:

for shetos in sheto:
     elementList = shetos.find_elements_by_tag_name("td")
     print(elementList.text )

【讨论】:

  • 所以,我修复了语法错误,删除了 [0] 以及它不起作用。将 h3 更改为 td (我正在寻找的标签)。执行第一个场景会提示此错误: elementList = sheto.find_elements_by_tag_name("td") AttributeError: 'str' object has no attribute 'find_elements_by_tag_name' 当我选择第二个时(对于 sheto 中的 shetos),我没有得到任何sheto 的输出,只是一个空格。
  • @ConfusedCode 忘记在末尾添加 .text
  • 是的,我自己加的……还是一样的结果
  • 你必须从 sheto 中删除它,因为如果你在 sheto 中使用 .text,标签就会消失,我确实更新了代码
  • AttributeError: 'list' object has no attribute 'text' - 非常抱歉,我要哭了:D
猜你喜欢
  • 2018-06-16
  • 1970-01-01
  • 2019-07-21
  • 1970-01-01
  • 2022-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-07
相关资源
最近更新 更多