Python/Selenium:从XPath(normalizespace)检索文本内容时出现空白问题

2024-09-29 21:58:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我在使用Seleniumfor Python的相对XPathweb scraper实现时遇到了一些困难。在

从这个Börse Frankfurt web page,我想得到<td> UCITS IV-Konform </td>相邻单元格中的文本,也就是说<td class="text-right"> Ja </td>的单元格中的文本。在

我已经测试了与Freeformatter一起使用的XPath,它声明我的XPath是正确的。在

导航到页面工作正常。但是,当我试图检索文本内容时,我得到None。显然,XPath不是在寻找。在

回答后编辑:问题是由于空格在文本内容的前面/后面。在


from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException

driver = webdriver.Firefox()
driver.get("http://www.boerse-frankfurt.de/etp/db-x-trackers-STOXX-GLOBAL-SELECT-DIVIDEND-100-UCITS-ETF-1D-LU0292096186")

try:
    find_value = driver.find_element_by_xpath("//td[text()=' UCITS IV-Konform ']/following-sibling::td").text
except NoSuchElementException:
    find_value = None

print find_value

Tags: textfrom文本importnone内容valuedriver
2条回答

尝试在xpath中使用contains函数:

"//td[contains(text(), 'UCITS IV-Konform')]/following-sibling::td"

有一个很好的解释here

试试XPath "//td[normalize-space(.) = 'UCITS IV-Konform']/following-sibling::td",因为我认为该单元格中有很多前导和尾随空格。

相关问题 更多 >

    热门问题