简体   繁体   English

如何使用Java使用Selenium Webdriver在此html中获取标记值?

[英]How to get the <b> tag value in this html with Selenium Webdriver using Java?

How can the Webdriver xpath function be used to get the tag value in the snippets below? 如何使用Webdriver xpath函数在下面的代码片段中获取标签值? The Java code is not working as expected. Java代码无法正常工作。

Here is the HTML: 这是HTML:

<p class="fontlarg">
    QC_NUM:<b>8300</b>
    </code>
</p>

Here is the Java code: 这是Java代码:

String valueOfbTag = driver.findElement(By.xpath("/p[1]/b[1]")).getText();

What you are using is an absolute xpath /p[1]/b[1] is expecting the root element in your xhtml to be p where I expect that you have html as your root element. 您正在使用的是绝对xpath /p[1]/b[1]期望xhtml中的根元素为p ,我希望您以html作为根元素。

To solve this you can make it relative //p[1]/b[1] this will find all p elements in your tree and then find the first b element inside the first p element. 为了解决这个问题,您可以使其相对//p[1]/b[1]这将找到树中的所有p元素,然后在第一个p元素内找到第一个b元素。

Te make this more specific you can select only the p elements with the correct class //p[@class='fontlarg'][1]/b[1] . 为了更具体一点,您只能选择具有正确类//p[@class='fontlarg'][1]/b[1]p元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM