作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试获取 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'>]
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
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
response.xpath('*/div[@class="outer"]/h2/text()="Chief Executive"/parent::*')
ValueError: XPath error: Invalid type in */div[@class="outer"]/h2/text()="Chief Executive"/parent::*
response.xpath('*/div[@class="outer"]/h2/text()="Chief Executive"/..')
ValueError: XPath error: Invalid type in */div[@class="outer"]/h2/text()="Chief Executive"/..
response.xpath('*/div[@class="outer"]/h2[.="Chief Executive"]')
[] # No data found
response.xpath('*/div[@class="outer"]/h2[text()="Chief Executive"]')
[] # No data found
<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
的条目中选择其他字段。 , 按照要求。
关于html - XPath 根据兄弟/表亲文本选择元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47877859/
我是一名优秀的程序员,十分优秀!