英文:
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, "productTitle")]/following-sibling::td
部分地对我起作用(获取所有元素,而不是我正在寻找的单个元素),而且它不符合我的需求,因为我需要与<strong>
元素的内容相关,以获取列标题。
英文:
On the URL I’m trying to get with XPath the content of certain HTML element, like on the screenshot:
I tried it with multiple XPaths and settings, like
//td[contains(@class, "productTitle")]/strong[contains(text(),"Movement:")]/following-sibling::td
//td[@class="productTitle"]/strong[text()="Movement:"]/following-sibling::td
But nothing worked for me.
Only the variant //td[contains(@class, "productTitle")]/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 <strong>
-element, to get the column heading.
答案1
得分: 2
当上下文为 strong
时,没有下一个兄弟节点 td
,所以你需要返回到父级。
示例(未经测试)...
//td[contains(@class, "productTitle")]/strong[contains(text(),"Movement:")]/../following-sibling::td
然而,我更倾向于基于第一个 td
选择正确的 tr
,然后选择第二个 tr
(同样未经测试)...
//tr[td[1][contains(@class,'productTitle')][contains(.,'Movement:')]]/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, "productTitle")]/strong[contains(text(),"Movement:")]/../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,'productTitle')][contains(.,'Movement:')]]/td[2]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论