简体   繁体   English

如何在没有 html 标签 selenium 的情况下获取文本

[英]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.

相关问题 如何从没有指定HTML标记的元素中获取文本 - How to get the text from element without specified HTML tag Selenium - 如何在图像标签后获取文本 - Selenium - How to get text after image tag 如何编写定位器并获取 selenium 自动化代码 HTML 代码中单个标签中的文本 - How to write a locator and get both the text present in a single tag in HTML code for selenium automation 无法使用Selenium Webdriver获取HTML TAG TEXT - Not able to get HTML TAG TEXT using Selenium webdriver 如何使用 Selenium 在 HTML 标记中查找没有值的属性是否存在? - How to find attribute without value is present or not in HTML tag using Selenium? 如何使用 Selenium 中的以下子项检索没有标签的元素的文本 - How to retrieve text of an element without tag using following child in Selenium 如何在HTML标记中选择文本而不在其周围添加标记(JSoup) - How to select text in HTML tag without a tag around it (JSoup) 如何使用Selenium WebDriver访问HTML图标标签内的动态文本? - How to access dynamic text within HTML icon tag with selenium WebDriver? 如何使用Java使用Selenium Webdriver在此html中获取标记值? - How to get the <b> tag value in this html with Selenium Webdriver using Java? 如何从selenium webdriver中的HTML获取标记名称 - How to get the tag Names from the HTML in selenium webdriver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM