如何获取没有html标签硒的文本

时间:2020-03-31 10:45:42

标签: java selenium xpath

Need to get text 6537

尝试了许多xpath:

driver.findElement(By.xpath("//b[contains(text(),'Client ID')]")).getText()

它只给文本主客户ID而不是6537。

如果我们将xpath更改为//b[contains(text(),'Client ID')]/text()

然后硒给出如下错误

The result of the xpath expression "//b[contains(text(),'Client ID')]/text()" is: [object Text]. It should be an element.

1 个答案:

答案 0 :(得分:0)

基于屏幕截图,您要捕获的文本似乎不属于标记的一部分。这是父元素的文本。

enter image description here

您需要做的是获取整个父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+","");