首页 > 解决方案 > Python - Selenium Xpath 在第一个和第二个 h3 标签之间获取内容

问题描述

结构:

<h3>Description</h3>
<p>Para1</p>
<p>Para1</p>
<p>Para1</p>
<p>Para1</p>
<h3>Description2</h3>
<p>Para1</p>
<p>Para1</p>
<p>Para1</p>
<p>Para1</p>

现在我想提取<p>first<h3>到 second之间的所有内容<h3>条件:

  1. 不使用标题内容检查。由于 Description2<h3>是动态内容,而不是恒定值。
  2. <h3>有时会<h1><h2>太...

尝试下面的通用代码,

One way
$x("//*[starts-with(name(),'h')][starts-with(.,'Description')]/following-sibling::*[following-sibling::*[starts-with(name(),'h')][starts-with(.,'Description2')]]")

Another
$x("//*[preceding-sibling::*[starts-with(name(),'h')] = 'Description' and following-sibling::*[starts-with(name(),'h')] = 'Description2']")

如何在不指定 Description & Description2 文本的情况下在内容之间进行查询?

标签: pythonseleniumxpath

解决方案


推荐阅读