简体   繁体   中英

How to get the partial text of class value that is inside a tag

I want to fetch the text 'completed' from the below html element

<div class="status">
      <span class="statusText completed"></span>
</div>

xpath: //span[@class='statusText completed']

based on the status -- 'completed', 'inprogress' or 'error' I want to proceed further. Please help me with this.

Note: There is no text for span element to retreive using 'getText()' method.

You need to use getAttribute() and get the class attribute value and then spilt it to get the last string value.

WebElement element =driver.findElement(By.xpath("//span[contains(@class,'statusText')]"));
String[] strele = element.getAttribute("class").split(" ");
String status = strele[strele.length-1];
System.out.println(status);
String status = driver.findElement(By.xpath("//div[@class='status']")).getText();
status= status.split(" ")[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