简体   繁体   中英

java - Extract text which contains <br> tag with Selenium Webdriver

I'm trying to extract text from following HTML code :

<div>
   "blabla"
   <br>
   "blublu"
   <br>
   "blibli"
</div>

I'm using getAttribute method because the text can be hidden (so getText() can possibly return null) :

String text = driver.findElement(By.tagName("div")).getAttribute("textContent");
System.out.println(text);

the expected result is

blabla\nblublu\nblibli

however I get

blablablublublibli

You can use getText() method on the WebElement

driver.findElement(By.xpath("//div")).getText()

Output would be something like:-

"blabla"
"blublu"
"blibli"

Problem resolved by using

String text = driver.findElement(By.tagName("div")).getAttribute("innerText");

and not

String text = driver.findElement(By.tagName("div")).getAttribute("textContent");

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