【发布时间】:2018-08-28 02:02:36
【问题描述】:
在这个page上,我希望 Selenium for Python 抓取“投资目标”的文本内容,不包括 <h3> 标头。我想使用 XPath。
节点如下所示:
<div class="carousel-content column fund-objective">
<h3 class="carousel-header">INVESTMENT OBJECTIVE</h3>
The Fund seeks to track the performance of an index composed of 25 of the largest Dutch companies listed on NYSE Euronext Amsterdam.
</div>
要检索文本,我正在使用:
string = driver.find_element_by_xpath(xpath).text
如果使用 I 这个 XPath 作为顶部节点:
xpath = '//div[@class="carousel-content column fund-objective"]'
它会起作用,但它包含 <h3> 标头 INVESTMENT OBJECTIVE — 我想排除它。
但是,如果我尝试使用 /text() 来处理实际的文本内容,似乎 Selenium for Python 不允许我在使用 .text 获取属性时抓取它:
xpath = '//div[@class="carousel-content column fund-objective"]/text()'
请注意,在此特定页面上似乎有多个具有此 XPath 的节点,因此我指定了正确的节点,如下所示:
xpath = '(//div[@class="carousel-content column fund-objective"]/text())[2]'
我对问题的解释是.text 不允许我检索XPath 子节点text() 的文本内容。对于不正确的术语,我深表歉意。
【问题讨论】:
标签: python python-2.7 selenium xpath