Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
529 views
in Technique[技术] by (71.8m points)

javascript - 如何使用Selenium和Java提取不在其自身标签内的文本节点的文本?(How to extract the text of a text node that is not inside a tag of its own using Selenium and Java?)

I want to get a single line of text (eg standard_user).

(我想获得一行文本(例如standard_user)。)

How can i do this?

(我怎样才能做到这一点?)

<div id="login_credentials" class="login_credentials">
              <h4>Accepted usernames are:</h4>

              standard_user
<br>
              locked_out_user
<br>
              problem_user
<br>
              performance_glitch_user<br>

            </div>
  ask by noon translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Collect all the text contained in one div, and separate them with break lines:

(收集一个div中包含的所有文本,并用换行符分隔它们:)

WebElement element = driver.findElement(By.id("login_credentials"));
String lines[] = element.getText().split("
");
System.out.println(lines[1]);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...