在Selenium中使用xpath获取正确的节点?

2024-04-19 21:23:23 发布

您现在位置:Python中文网/ 问答频道 /正文

访问此link

我想得到product material & care的细节。你知道吗

Shell: 70% polyester, 30% wool
Lining: 100% polyester
Dry-clean.

我试过下面提到的代码,但它不适合我。你知道吗

self.browser.find_element_by_xpath("//h6[@class='pdp-product-description-title']/p").text

Tags: 代码selfbrowsercleanlinkfindproductshell
2条回答

<p>标记是同级元素,而不是子元素。使用following-sibling::p

self.browser.find_element_by_xpath("//h6[@class='pdp-product-description-title']/following-sibling::p").text

这样试试。你知道吗

解释xpath:-使用<h6>标记和text方法开始xpath,然后使用following关键字继续使用<p>标记。你知道吗

self.browser.find_element_by_xpath("//h6[contains(text(), 'Material & Care')]/following::p[@class='pdp-product-description-content']").text

OR

使用带有<p>标记的索引。你知道吗

self.browser.find_element_by_xpath("//h6[contains(text(), 'Material & Care')]/following::p[1]").text

相关问题 更多 >