Get formatted text with Selenium Webdriver in Java

Viewed 1850

Extracting the text from the following HTML code, with getText() method, is not working as I expected.

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:

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:

Text extracted:
RP/ABCDEFGHI/
1.TESTING/UI

The rest, "2 PHONE NUMBER" and "3 SOME FREE TEXT" are not displayed.

*The text is read line by line from a .txt file, and after each read line, sendKeys(Keys.ENTER) is used. 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!

2 Answers
Related