Python 选择前面/后面的同级XPath
我将Selenium与Python一起使用,我想在hr标记之前选择html。 以下是我的代码:Python 选择前面/后面的同级XPath,python,selenium,xpath,Python,Selenium,Xpath,我将Selenium与Python一起使用,我想在hr标记之前选择html。 以下是我的代码: <div id="wikipage"> <div id="wikipage-inner"> <h1>Berkeley</h1> <p><span><strong>Title1</strong></span></p> <p><strong>Address: &l
<div id="wikipage">
<div id="wikipage-inner">
<h1>Berkeley</h1>
<p><span><strong>Title1</strong></span></p>
<p><strong>Address: </strong>..</p>
<p><strong>Website: </strong><a href="..">..</a></p>
<p><strong>Phone: </strong>..</p>
<hr />
<p><strong><span">Title2</span></strong></p>
<p><strong>Address: </strong>..</p>
<p><strong>Website:</strong> <a href="..">..</a></p>
<p><strong>Phone:</strong> ..</p>
<p><strong>Email:</strong> <a href="mailto:..">..</a></p>
<hr />
</div>
</div>
写作时:
for p in parag:
print('This is a text before hr: ', p.text)
我将感谢一些帮助人员。如果您有固定数量的
节点,您可以尝试以下xpath:
//hr[x]/前面的兄弟姐妹::p[position()你想选择哪个HTML元素???你能解释更多吗?你想在
之前选择所有
?…并且有两个hr
元素。哪一个?你尝试过什么XPath表达式?结果是什么,你想要的确切结果是什么?@LarsH我对XPath和下面的内容都不太熟悉-兄弟姐妹,它以前对我来说做得很好,现在我想到了前面的兄弟姐妹,我不知道是否有办法实现我在这里想要的…没有尝试太多…谢谢你,它按预期工作,我在顶部和底部没有人力资源标签,也许我可以使用这个解决方案。这是我拥有的最好的解决方案。也许我可以使用BeautifulSoup?
for p in parag:
print('This is a text before hr: ', p.text)
//hr[x]/preceding-sibling::p[position()<=y]
//hr[2]/preceding-sibling::p[position()<=5]
//hr[x]/preceding-sibling::p[position()<=count(//hr[x]/preceding-sibling::p) - count(//hr[y]/preceding-sibling::p)]
//hr[2]/preceding-sibling::p[position()<=count(//hr[2]/preceding-sibling::p) - count(//hr[1]/preceding-sibling::p)]