java - 如何使用Java(用于硒自动化)获取段落标签内的文本。(div/div/p)
问题描述
我正在尝试使用 xpath 从模型中获取文本,但最终没有得到文本,但我可以获得标签名称。
HTML:
<div class="modal fade custom-modal" id="loginfailure-modal" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-body">
<p id="custom-modal-text">We could not find that<br> account. Incorrect email and /<br> or password</p>
<button id="custom-modal-btntext" type="button" class="button green-button custom-modal-button" data-dismiss="modal">Please try again</button>
</div>
</div>
</div>
</div>
代码试用:
String txt = driver.findElement(By.xpath("//p[@id='custom-modal-text']")).getText();
我想要标签内的文本<p>
但没有得到它,但我可以使用 get tag name 函数获取标签名称。
解决方案
要提取文本We could not find that ... 或 password因为元素位于模态对话框中,您需要为visibilityOfElementLocated引入WebDriverWait,您可以使用以下任一解决方案:
选择器:
String txt = driver.findElement(By.cssSelector("div.modal.fade.custom-modal#loginfailure-modal div.modal-body>p")).getText();
路径:
String txt = driver.findElement(By.xpath("//div[@class='modal fade custom-modal' and @id='loginfailure-modal']//div[@class='modal-body']/p")).getText();
推荐阅读
- django - 如何在 django 中编辑模板时显示选中的项目和上传的图像值
- javafx - 如何在 javafx 中打开带有标签、文本字段等的新窗口?
- wordpress - 如何将占位符图标放入联系表格 7?
- html - HTML 输入模式属性
- node.js - 由于安装位置已被删除,无法全局卸载此 node.js 包
- c# - 在列表框中选择项目时尝试显示数据集中行的所有值
- python - 如何强制 Python (EMCEE) 在先前定义的范围之间精确移动
- laravel - 如何在不同的 Laravel 控制器中重用自定义函数?
- spring-mvc - java.lang.IllegalStateException:没有为 servlet 请求找到匹配的处理程序方法
- php - Codeigniter - 使用 MySql 从具有多个重复 MaxDate/time 的元表中获取数据