gpt4 book ai didi

python - 使用 Selenium + Python 获取 text() 节点值

转载 作者:太空宇宙 更新时间:2023-11-04 10:41:32 25 4
gpt4 key购买 nike

电话号码是一个元素,需要获取里面的文字。

<a href="tel:895**49****" class="button-text action-link" title="Телефон продавца" rel="nofollow">
"8 9** **9-99-**"
</a>

当我使用

phone = driver.find_element_by_class_name('button-text')
print phone.text

它返回一个空字符串,因为“”中的电话号码是一个 text() 节点。

当我尝试的时候

print driver.find_element_by_xpath('/html/body/section/article/section[2]/ul/li[1]/a/text()')

或者这个

print driver.find_element_by_xpath('/html/body/section/article/section[2]/ul/li[1]/a/text()').text

它返回这个错误:

InvalidSelectorException: Message: u'Error Message => 'The result of the xpath expression "/html/body/section/article/section[2]/ul/li[1]/a/text()" is: [object Text]. It should be an element.

最佳答案

您必须指定 XPath 表达式以返回元素,而不是文本。因为 Selenium 与元素一起工作。

  1. 首先定位元素(使用 XPath 或 CSS 选择器,...)
  2. 然后在元素上调用方法以返回其文本

这应该有效:

print driver.find_element_by_xpath('/html/body/section/article/section[2]/ul/li[1]/a').text

注意:XPath 表达式末尾不能包含函数 text()(或类似函数),因为它会导致返回文本并且 Selenium 需要元素。 Java 中的 Selenium 2 ( WebDriver) 也是如此。

关于python - 使用 Selenium + Python 获取 text() 节点值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20375096/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com