我想只得到"邀请发送到xxxxx"的文字,我不想得到里面的内容
<button class="action" data-ember-action="" data-ember-action-3995="3995">
Visualizar perfil
</button>
Run Code Online (Sandbox Code Playgroud)
我这样得到文字:
String pessoapopu = driver.findElement(By.className("artdeco-toast-message")).getText();
System.out.println(pessoapopu);
Run Code Online (Sandbox Code Playgroud)
页面结构:
<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>
Run Code Online (Sandbox Code Playgroud)
您可以使用//p[@class='artdeco-toast-message']/text()xpath来定位Invitation sent to xxxxx文本,但selenium不支持text()xpath中的方法来定位文本节点.
如果您尝试使用xpath下面的元素来定位元素,则使用xpath not()函数排除按钮文本:
//p[@class='artdeco-toast-message']/node()[not(self::button)]
Run Code Online (Sandbox Code Playgroud)
它再次使用文本节点定位元素,因此Selenium不允许这样做
这里有一个可用于执行相同xpath的解决方案 JavascriptExecutor
使用evaluate()方法JavaScript和评估你的xpathJavascriptExecutor
JavascriptExecutor js = (JavascriptExecutor)driver;
Object message = js.executeScript("var value = document.evaluate(\"//p[@class='artdeco-toast-message']/text()\",document, null, XPathResult.STRING_TYPE, null ); return value.stringValue;");
System.out.println(message.toString().trim());
Run Code Online (Sandbox Code Playgroud)
要么
JavascriptExecutor js = (JavascriptExecutor)driver;
Object message = js.executeScript("var value = document.evaluate(\"//p[@class='artdeco-toast-message']/node()[not(self::button)]\",document, null, XPathResult.STRING_TYPE, null ); return value.stringValue;");
System.out.println(message.toString().trim());
Run Code Online (Sandbox Code Playgroud)
它会给你预期的结果.无需获取所有数据,然后使用String函数进行格式化.
您可以从这里evaluate()进行详细探索
| 归档时间: |
|
| 查看次数: |
826 次 |
| 最近记录: |