简体   繁体   中英

How To Get get data(Claim number and Status) from HTML using java

In my application, I have submitted a claim It generated the claim details along with claim number and Status. I need to extract the Claim Number and Status from the claim details.

HTML Code for the table containing Claim number and Status:

<div id="claim-num-success" style="width:50%; margin:0 auto; padding:25px; background:none; border:1px solid #d3d3d3; line-height:24px;"> <b>Service Name:</b> 7,500 MILES - NON-TURBO ENGINE
  <br> <b>Claim Number:</b> 02923240
  <br> <b>R/O Number:</b> 12000
  <br> <b>R/O Date:</b> 12/13/2017
  <br> <b>Claim Amount:</b> $40.00
  <br> <b>Status:</b> APPROVED
  <br> 
</div>

Here you can use below xpath to extract the value :

//div[@id='claim-num-success']/b[text()='Claim Number:']/following-sibling::text()[1]

AND

//div[@id='claim-num-success']/b[text()='Status:']/following-sibling::text()[1]

But Selenium doesn't allow you to locate an element using text node in xpath. So you can use JavascriptExecutor to evaluate your xpath and locate the element using text node.

This is how you can full-fill your requirement :

JavascriptExecutor js = (JavascriptExecutor)driver;
Object claimNo= js.executeScript("var value = document.evaluate(\"//div[@id='claim-num-success']/b[text()='Claim Number:']/following-sibling::text()[1]\",document, null, XPathResult.STRING_TYPE, null ); return value.stringValue;");
System.out.println("Claim Number : "+ claimNo.toString());

Object Status= js.executeScript("var value = document.evaluate(\"//div[@id='claim-num-success']/b[text()='Status:']/following-sibling::text()[1]\",document, null, XPathResult.STRING_TYPE, null ); return value.stringValue;");
System.out.println("Status : "+ Status.toString());

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