简体   繁体   中英

WebDriver getText() Method with replace using Java

Html Code

<span data-bind="html: TotalCharges">
<span class="CurrencySymbol">USD </span>
7400.00
<br>
(0.00+0.00)
</span>

Webdriver to get the Totalcharge value using getText method

Code:

driver.findElement(By.xpath("//span[@data-bind='html: TotalCharges']")).getText().substring(4);

with above will get the below output "7400.00 (0.00+0.00)"

my Expected output :"7400.00"

so how can i replace the char from "< br>" tag (need to replace "(0.00+0.00)") i'm using java

Use the following xpath to get 7400.00:

driver.findElement(By.xpath("//span[@class='CurrencySymbol']/following-sibling::text()[1]").getText();

Oh My mistake, Thanks for correcting me @alecxe: You can get it by:

driver.findElement(By.xpath("//span[@class='CurrencySymbol']/.."))
        .getText().split("\n")[0].split(" ")[1]

splitting at \\n will split it for <br> tag.

Try following solution. It will give you 7400.00 output-

String temp = driver.findElement(By.cssSelector("html>body>span")).getText();
String s1=temp.replace("USD", "").replace("\n", "").replace("\r", "");
String finalStr = s1.substring(0,s1.indexOf("(")).trim();
System.out.println(finalStr);

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