使用java + selenium WebDriver获取一些文本 [英] Get some text with java + selenium WebDriver
本文介绍了使用java + selenium WebDriver获取一些文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想只获得文字邀请函发送到xxxxx,我不想得到
I want to get only the text "Invitation sent to xxxxx", I do not want to get what's inside the
<button class="action" data-ember-action="" data-ember-action-3995="3995">
Visualizar perfil
</button>
我以这种方式获取文字:
String pessoapopu = driver.findElement(By.className("artdeco-toast-message")).getText();
System.out.println(pessoapopu);
页面结构:
<div class="artdeco-toast-inner" data-ember-action="" data-ember-action-3994="3994">
<li-icon aria-hidden="true" type="success-pebble-icon" class="artdeco-toast-icon"><svg viewBox="0 0 24 24" width="24px" height="24px" x="0" y="0" preserveAspectRatio="xMinYMin meet" class="artdeco-icon"><g class="large-icon" style="fill: currentColor">
<g id="success-pebble-icon">
<g>
<circle class="circle" r="9.1" stroke="currentColor" stroke-width="1.8" cx="12" cy="12" fill="none" transform="rotate(-90 12 12)"></circle>
<path d="M15.667,8L17,9.042l-5.316,7.36c-0.297,0.395-0.739,0.594-1.184,0.599c-0.455,0.005-0.911-0.195-1.215-0.599l-2.441-3.456l1.416-1.028l2.227,3.167L15.667,8z" fill="currentColor"></path>
<rect style="fill:none;" width="24" height="24"></rect>
</g>
</g>
<g id="Layer_1">
</g>
</g></svg></li-icon>
<p class="artdeco-toast-message">
Invitation sent to xxxxx
<button class="action" data-ember-action="" data-ember-action-3995="3995">
Visualizar perfil
</button>
</p>
</div>
推荐答案
实际上Selenium的getText()方法也会检索子元素中的所有文本,因此 Murthi 推荐的方法是最适用的,目前为止我所知。
尝试这种方法:
In fact Selenium's getText() method retrieves all the text in sub elements too, so the approach, recommended by Murthi is the most applicable, as far as I know. Try this approach:
String newLine = System.getProperty("line.separator");
String pessoapopu = driver.findElement(By.className("artdeco-toast-message"))
.getText().replaceAll(newLine, "");
或尝试/询问更方便的HTML代码。
Or try/ask for more convenient HTML code.
这篇关于使用java + selenium WebDriver获取一些文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文