我在使用Selenium
for Python
的相对XPath
web 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
尝试在xpath中使用
contains
函数:有一个很好的解释here。
试试XPath
"//td[normalize-space(.) = 'UCITS IV-Konform']/following-sibling::td"
,因为我认为该单元格中有很多前导和尾随空格。相关问题 更多 >
编程相关推荐