硒,元素,整个<li>

弗兰基

我需要从包含单词“ Produktgewicht”的网页中找到元素

这是代码用来查找元素的代码:

String Weight=wd.findElement(By.xpath("//*[contains(text(), 'Produktgewicht')]")).getText();

这是网页的源代码:

<li>
    <b>
        Produktgewicht inkl. Verpackung:
    </b>
     6 Kg
</li>

我的代码的输出是“ Produktgewicht inkl。Verpackung:”

但我要整个 <li>

这是我想要的输出“ Produktgewicht inkl。Verpackung:6 Kg”

我什至尝试过

String Weight=wd.findElement(By.xpath("//*[contains(text(), 'Produktgewicht')]/following::li")).getText();

但这给了我下一个 <li>

请帮帮我.....在此先感谢:)

har07

您可以这样尝试:

//li[contains(., 'Produktgewicht')]

<li>无论嵌套在中的文本有多深,上面的返回值都应包含'Produktgewicht'文本<li>与此相比,*[contains(text(), 'Produktgewicht')]仅测试第一个直接子文本节点。

/following::li您尝试的操作不起作用,因为<li>未遵循先前匹配的<b>元素,它是<b>代替的父项

//*[contains(text(), 'Produktgewicht')]/parent::li

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章