【发布时间】:2015-02-19 21:28:18
【问题描述】:
我有这个 html:
<div id="content">
<h1>Title 1</h1><br><br>
<h2>Sub-Title 1</h2>
<br><br>
Description 1.<br><br>Description 2.
<br><br>
<h2>Sub-Title 2</h2>
<br><br>
Description 1<br>Description 2<br>
<br><br>
<div class="infobox">
<font style="color:#000000"><b>Information Title</b></font>
<br><br>Long Information Text
</div>
</div>
我想在 Selenium 的 find_element_by_xpath 函数中获取 <div id="content"> 中的所有文本但不包括 <div class="infobox"> 的内容,所以预期的结果是这样的:
Title 1
Sub-Title 1
Descripton 1.
Descripton 2.
Sub-Title 2
Descripton 1.
Descripton 2.
我可以通过在在线 XPath 测试器中使用此代码来获得它:
//div[@id="content"]/descendant::text()[not(ancestor::div/@class="infobox")]
但是如果我将代码传递给 selenium 的 find_element_by_xpath,我会得到selenium.common.exceptions.InvalidSelectorException。
result = driver.find_element_by_xpath('//div[@id="content"]/descendant::text()[not(ancestor::div/@class="infobox")]')
【问题讨论】:
标签: python html selenium xpath selenium-webdriver