XPath根据兄弟/堂兄文本选择元素?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了XPath根据兄弟/堂兄文本选择元素?相关的知识,希望对你有一定的参考价值。
我试图刮掉Chief Executive
和Someone Else
的联系方式
我可以使用以下代码找到行政长官
response.xpath('*/div[@class="outer"]/h2/text()="Chief Executive"')
# Returns a Selector
[<Selector xpath='*/div[@class="outer"]/h2/text()="Chief Executive"' data=u'0'>]
但是一旦我尝试访问父母或兄弟姐妹,我就会收到错误或没有数据。
以下是我尝试过的一些模式
1.
response.xpath('*/div[@class="outer"]/h2/text()="Chief Executive"/following-sibling')
ValueError: XPath error: Invalid type in */div[@class="outer"]/h2/text()="Chief Executive"/following-sibling
2.
response.xpath('*/div[@class="outer"]/h2/text()="Chief Executive"/following-sibling::content')
ValueError: XPath error: Invalid type in */div[@class="outer"]/h2/text()="Chief Executive"/following-sibling::content
3.
response.xpath('*/div[@class="outer"]/h2/text()="Chief Executive"/parent::*')
ValueError: XPath error: Invalid type in */div[@class="outer"]/h2/text()="Chief Executive"/parent::*
4
response.xpath('*/div[@class="outer"]/h2/text()="Chief Executive"/..')
ValueError: XPath error: Invalid type in */div[@class="outer"]/h2/text()="Chief Executive"/..
5
response.xpath('*/div[@class="outer"]/h2[.="Chief Executive"]')
[] # No data found
6
response.xpath('*/div[@class="outer"]/h2[text()="Chief Executive"]')
[] # No data found
基础html
<div class="outer">
<h2 class="legend">
Chief Executive
</h2>
<div class="fieldset">
<div class="display-row">
<div class="display-label">Contact name:</div>
<div class="display-field-no-width">
Mr. Steven Bob
</div>
</div>
<div class="display-row">
<div class="display-label">Job title:</div>
<div class="display-field-no-width">
Chief Executive Officer
</div>
</div>
<div class="display-row">
<div class="display-label">Organisation name:</div>
<div class="display-field-no-width">
1 COMAPNY PTY LTD
</div>
</div>
</div>
</div>
<div class="outer">
<h2 class="legend">
Someone Else
</h2>
<div class="fieldset">
<div class="display-row">
<div class="display-label">Contact name:</div>
<div class="display-field-no-width">
Mr. Steven Bob
</div>
</div>
<div class="display-row">
<div class="display-label">Job title:</div>
<div class="display-field-no-width">
Chief Executive Officer
</div>
</div>
<div class="display-row">
<div class="display-label">Organisation name:</div>
<div class="display-field-no-width">
1 COMAPNY PTY LTD
</div>
</div>
</div>
</div>
答案
这个XPath,
normalize-space(//div[normalize-space(h2)='Chief Executive']
/div[div[1]='Contact name:'])
将返回
Mr. Steven Bob
按照此模式,您可以根据要求从此条目或Someone Else
条目中选择其他字段。
以上是关于XPath根据兄弟/堂兄文本选择元素?的主要内容,如果未能解决你的问题,请参考以下文章
追求最优美的代码 leetcode 993. 二叉树的堂兄弟节点
追求最优美的代码 leetcode 993. 二叉树的堂兄弟节点
appium xpath找兄弟元素 根据二师兄找大师兄 语法:driver.find_element_by_xpath("//*[@text='测听报告列表']