following-sibling 与类似 strong 的元素如何配合使用?

huangapple go评论60阅读模式
英文:

How works following-sibling with elements like strong?

问题

I'm here to assist with the Chinese translation. Here is the translated content:

URL上,我正在尝试使用XPath获取特定HTML元素的内容,就像屏幕截图中的内容:

我尝试了多个XPath和设置,如下所示:

//td[contains(@class, "productTitle")]/strong[contains(text(),"Movement:")]/following-sibling::td

//td[@class="productTitle"]/strong[text()="Movement:"]/following-sibling::td

但都没有对我起作用。

只有//td[contains(@class, &quot;productTitle&quot;)]/following-sibling::td部分地对我起作用(获取所有元素,而不是我正在寻找的单个元素),而且它不符合我的需求,因为我需要<strong>元素的内容相关,以获取列标题。

英文:

On the URL I’m trying to get with XPath the content of certain HTML element, like on the screenshot:
following-sibling 与类似 strong 的元素如何配合使用?

I tried it with multiple XPaths and settings, like

//td[contains(@class, &quot;productTitle&quot;)]/strong[contains(text(),&quot;Movement:&quot;)]/following-sibling::td

//td[@class=&quot;productTitle&quot;]/strong[text()=&quot;Movement:&quot;]/following-sibling::td

But nothing worked for me.

Only the variant //td[contains(@class, &quot;productTitle&quot;)]/following-sibling::td worked partly for me (getting all elements, and not the single one I'm looking for), and it doesn’t match my need, because I need the relation on the content of the &lt;strong&gt;-element, to get the column heading.

答案1

得分: 2

当上下文为 strong 时,没有下一个兄弟节点 td,所以你需要返回到父级。

示例(未经测试)...

//td[contains(@class, &quot;productTitle&quot;)]/strong[contains(text(),&quot;Movement:&quot;)]/../following-sibling::td

然而,我更倾向于基于第一个 td 选择正确的 tr,然后选择第二个 tr(同样未经测试)...

//tr[td[1][contains(@class,&#39;productTitle&#39;)][contains(.,&#39;Movement:&#39;)]]/td[2]
英文:

There is no following-sibling td when the context is strong, so you'd have to go back up to the parent.

Example (untested)...

//td[contains(@class, &quot;productTitle&quot;)]/strong[contains(text(),&quot;Movement:&quot;)]/../following-sibling::td

However, I would prefer selecting the correct tr based on the first td and then selecting the second tr (also untested)...

//tr[td[1][contains(@class,&#39;productTitle&#39;)][contains(.,&#39;Movement:&#39;)]]/td[2]

huangapple
  • 本文由 发表于 2023年5月23日 01:30:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76308640.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定