我想在这个page的标题中获取文本:
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终端只打印一个空字符串:""
这可能是什么原因?
注意:我还尝试了一些其他的xpath替代方案,获得相同的结果,例如:
xp_name = ".//*[@id='fundHeader']//h1"
问题是有两个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