简体   繁体   中英

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.

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!

You can try using innerText attribute

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

<pre id="responseCommand"> 

is created, wraped into a div tag. So, for the first TESTING/UI line entered

<div class="textResponse0">
  <pre id="responseCommand">
    <code>RP/NCE1A0955/
     1.TESTING/UI</code>
  </pre>
</div>

for the second line PHONE NUMBER entered,

<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. Hence the absence of the other lines.

I did not have discovered this without your comments (inneHTML opened my eyes)

Thank you very much

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM