简体   繁体   中英

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? The Java code is not working as expected.

Here is the HTML:

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

Here is the Java code:

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.

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.

Te make this more specific you can select only the p elements with the correct class //p[@class='fontlarg'][1]/b[1] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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