Python Selenium:查找h1元素但返回空文本字符串

问题描述 投票:7回答:1

我想在这个page的标题中获取文本:

enter image description here

iShares FTSE MIB UCITS ETF EUR(Dist)

标签看起来像这样:

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

我正在使用这个xPath:

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

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

new_name = driver.find_element_by_xpath(xp_name).text

驱动程序找到xpath,但是当我打印new_name时,macOS终端只打印一个空字符串:""

这可能是什么原因?

enter image description here


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

xp_name = ".//*[@id='fundHeader']//h1"
python selenium xpath hidden
1个回答
12
投票

问题是有两个h1元素与完全相同的外部HTML:第一个是隐藏的,第二个不是。你可以检查一下

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

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

尝试更换

new_name = driver.find_element_by_xpath(xp_name).text

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

或者只是处理第二个(可见)标题:

driver.find_elements_by_xpath('//h1[@class="product-title "]')[1].text
© www.soinside.com 2019 - 2024. All rights reserved.