本文介绍了Python Selenium:查找h1元素,但返回空文本字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取此页面:

标签看起来像这样:

<h1 class="product-title" title="iShares FTSE MIB UCITS ETF EUR (Dist)"> iShares FTSE MIB UCITS ETF EUR (Dist) </h1>

我正在使用此xPath:

I am using this xPath:

xp_name = ".//*[@class[contains(normalize-space(.), 'product-title')]]"

在Selenium WebDriver for Python中通过.text检索:

Retrieving via .text in Selenium WebDriver for Python:

new_name = driver.find_element_by_xpath(xp_name).text

驱动程序找到xpath,但是当我打印new_name时,macOS Terminal仅打印空白字符串:""

The driver finds the xpath, but when I print new_name, macOS Terminal only prints a blank string: ""

这可能是什么原因?

注意:我还尝试了其他xpath替代方法,获得了相同的结果,例如:

Note: I also tried some other xpath alternatives, getting the same result, for example with:

xp_name = ".//*[@id='fundHeader']//h1"

推荐答案

问题是,有两个h1元素的外部HTML完全相同:第一个是隐藏的,第二个不是.您可以使用

The problem is that there are two h1 elements with totally the same outer HTML: the first is hidden, the second is not. You can check it with

print(len(driver.find_elements_by_xpath('//h1[@class="product-title "]')))

text属性允许您从仅可见元素中获取文本,而textContent属性还允许从隐藏的文本

text property allow you to get text from only visible elements while textContent attribute also allow to get text of hidden one

尝试替换

new_name = driver.find_element_by_xpath(xp_name).text

new_name = driver.find_element_by_xpath(xp_name).get_attribute('textContent')

或仅处理第二个(可见)标头:

or simply handle the second (visible) header:

driver.find_elements_by_xpath('//h1[@class="product-title "]')[1].text

这篇关于Python Selenium:查找h1元素,但返回空文本字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 15:42