我正在尝试使用 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>
内的文本标签,但我没有得到它,但我可以使用获取标签名称函数获取标签名称。
最佳答案
要提取文本我们找不到该...或密码,因为该元素位于模态对话框中,您需要引发WebDriverWait> 对于 visibilityOfElementLocated,您可以使用以下任一解决方案:
css选择器:
String txt = driver.findElement(By.cssSelector("div.modal.fade.custom-modal#loginfailure-modal div.modal-body>p")).getText();
xpath:
String txt = driver.findElement(By.xpath("//div[@class='modal fade custom-modal' and @id='loginfailure-modal']//div[@class='modal-body']/p")).getText();
关于java - 如何使用Java(用于selenium自动化)获取段落标签内的文本(div/div/p),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54322075/