Python XPath - 查找包含文本 A 或文本 B 的元素的兄弟元素

Python XPath - find sibling of element containing Text A OR Text B

同学们,

我正在抓取一些下载内容 link。在目标网站上,每个目标下载 link 有时会出现在文本 'Application Proof (1st submission)' 之后,有时会出现在文本 'PHIP (1st revised proof)'[= 之后31=]。我当前的代码仅抓取 'Application Proof (1st submission)':

之后的 links
all_proofs = driver.find_elements_by_xpath("//span[contains(text(),'Application Proof (1st submission)')]/following-sibling::a[contains(.,'Full Version')]")

有没有什么方法可以在此 XPath 中使用 OR 逻辑在这两种情况下进行抓取并根据它们在网站源代码中出现的顺序获得 link 的单个列表?逻辑大概是:

all_proofs = driver.find_elements_by_xpath("//span[contains(text(),'Application Proof (1st submission)' OR 'PHIP (1st revised proof)')]/following-sibling::a[contains(.,'Full Version')]")

遗憾的是,没有其他解决方法可以替代此逻辑,因为:

  1. 我不能简单地抓取所有包含 'Full Version' 的下载 link,因为其中一些 link 不符合我的标准,只有文本后面的那些'Application Proof (1st submission)' 'PHIP (1st revised proof)' 做。
  2. 我无法抓取 link 中跟在 'Application Proof (1st submission)' 文本之后的一个列表,然后再抓取 另一个 links 跟随 'PHIP (1st revised proof)' 最后将它们连接在一起,因为我需要此列表的顺序与 links 在网站源代码中出现的顺序完全相同.

感谢您的帮助!

是的,您可以在 XPath 中使用 OR 运算符。
您的 XPath 表达式可能是这样的:

all_proofs = driver.find_elements_by_xpath("//span[contains(text(),'Application Proof (1st submission)') or contains(text(),'PHIP (1st revised proof)')]/following-sibling::a[contains(.,'Full Version')]")