简体   繁体   English

如何获取标签内 class 值的部分文本

[英]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我想从下面的 html 元素中获取文本“完成”

<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.注意:没有使用“getText()”方法检索 span 元素的文本。

You need to use getAttribute() and get the class attribute value and then spilt it to get the last string value.您需要使用getAttribute()并获取 class 属性值,然后将其溢出以获取最后一个字符串值。

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];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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