[英]Get formatted text with Selenium Webdriver in Java
Extracting the text from the following HTML code, with getText() method, is not working as I expected. 使用getText()方法从以下HTML代码中提取文本无法正常工作。
HTML HTML
<pre id="responseCommand"><code>RP/ABCDEFGHI/
1.TESTING/UI
2 PHONE NUMBER
3 SOME FREE TEXT</code></pre>
Java code, in order to extract the text from code tag, is the following: 为了从代码标签中提取文本,Java代码如下:
WebDriverWait waitForElement = new WebDriverWait(driver, 20);
WebElement recoverText = waitForElement.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("pre[id='responseCommand'] code")));
recoverText= driver.findElement(By.cssSelector("pre[id='responseCommand'] code"));
String textStr = recoverText.getText();
System.out.println("Text extracted: \n" + textStr );
The output of the println is the following: println的输出如下:
Text extracted:
RP/ABCDEFGHI/
1.TESTING/UI
The rest, "2 PHONE NUMBER" and "3 SOME FREE TEXT" are not displayed. 其余的“ 2 PHONE NUMBER”和“ 3 SOME FREE TEXT”不显示。
*The text is read line by line from a .txt file, and after each read line, sendKeys(Keys.ENTER) is used. *从.txt文件逐行读取文本,并在每行读取之后使用sendKeys(Keys.ENTER)。 That's why, the text in the code tag is displayed on multiple lines.
因此,代码标记中的文本显示在多行上。
Despite all the documentation that is available, I could not managed to extract the entire text. 尽管有所有可用的文档,但我无法提取全部文本。
Any thoughts? 有什么想法吗?
Cheers! 干杯!
You can try using innerText
attribute 您可以尝试使用
innerText
属性
WebElement recoverText = waitForElement.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("pre[id='responseCommand'] code")));
String textStr = recoverText.getAttribute("innerText");
I finally found why I only have half of the text displayed. 我终于找到了为什么我只显示一半的文本。 Apparently, each time I am doing a keys.ENTER, a new
显然,每次我做一个key.ENTER时,
<pre id="responseCommand">
is created, wraped into a div tag. 创建后,包装到div标签中。 So, for the first TESTING/UI line entered
因此,对于输入的第一条TESTING / UI行
<div class="textResponse0">
<pre id="responseCommand">
<code>RP/NCE1A0955/
1.TESTING/UI</code>
</pre>
</div>
for the second line PHONE NUMBER entered, 输入的第二行PHONE NUMBER,
<div class="textResponse1">
<pre id="responseCommand">
<code>RP/NCE1A0955/
1.TESTING/UI
2 PHONE NUMBER</code>
</pre>
</div>
and so on... 等等...
My getText() is applied only on the first pre tag. 我的getText()仅应用于第一个pre标签。 Hence the absence of the other lines.
因此,没有其他线。
I did not have discovered this without your comments (inneHTML opened my eyes) 没有您的评论,我并没有发现这一点(inneHTML睁开了我的眼睛)
Thank you very much 非常感谢你
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.