[英]How to get text without html tag selenium
Need to get text 6537需要获取文本 6537
Tried many xpaths:尝试了很多 xpath:
driver.findElement(By.xpath("//b[contains(text(),'Client ID')]")).getText()
It just gives text Master Client Id not 6537.它只提供文本主客户端 ID 而不是 6537。
if we change xpath to //b[contains(text(),'Client ID')]/text()
如果我们将 xpath 更改为
//b[contains(text(),'Client ID')]/text()
Then selenium gives error as below然后硒给出如下错误
The result of the xpath expression "//b[contains(text(),'Client ID')]/text()" is: [object Text]. It should be an element.
Based on the screenshot it seems like the text you are trying to capture is not part of the tag.根据屏幕截图,您尝试捕获的文本似乎不是标签的一部分。 It is the text of the parent element.
它是父元素的文本。
What you need to do is to have get the text of the entire parent div and use a regex to extract the number out.您需要做的是获取整个父 div 的文本并使用正则表达式提取数字。
String parentText = driver.findElement(By.xpath("//b[contains(text(),'Client ID')]")).getText()
// find and remove any non digit characters
String number = parentText.replaceAll("\\D+","");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.