Home > Back-end >  Xpath from a span text in break
Xpath from a span text in break

Time:09-17

I am trying to get the validate the $40 by asserting but unable to track the xpath. Any Suggestions

<div class="MuiGrid-root MuiGrid-item MuiGrid-grid-xs-12"><div class="vetting-details"><h3 class="paragraph text-center"><span>Vetting Price: </span>$40 <br> <span>Estimated Time for Vetting:</span> 30 seconds</h3></div></div>

CodePudding user response:

That 40 is basically a text node. You can retrieve it using :

WebElement e = driver.findElement(By.xpath("//h3[contains(@class,'paragraph text-center')]"));
String el = (String)((JavascriptExecutor)driver).executeScript("return arguments[0].textContent;", e);
String s  = el.split("\\ ")[2].trim();
System.out.println(s)

Explanation :

40 is not a plain text rather it is a text node. we need JS intervention to target the p tag, and then get all the text content. Then using split to split the string to get the desired element.

CodePudding user response:

We can use below code (without JavaScriptExecutor)

 String value=driver.findElement(By.tagName("h3")).getText();
        String s1=value.split(":")[1].trim();
        System.out.println(s1);
  • Related