java - 我需要使用 Java 读取 Web 驱动程序中弹出窗口中显示的文本

标签 java javascript webdriver selenium-webdriver

Popupwindow

我需要使用 Java 读取 Web 驱动程序中弹出窗口中显示的文本。我能够处理关闭的弹出窗口。我不知道如何读取弹出窗口中显示的文本并在控制台中打印。

我无法为此提供任何 HTML 代码,因为它是一个模态弹出窗口。

请帮我解决这个问题。我们将不胜感激。

最佳答案

根据您的屏幕截图,您尝试自动化的“模态弹出窗口”似乎是由 JavaScript alert() 函数生成的。如果是这种情况,下面的代码或类似的代码应该可以工作。

// WARNING! Untested code written from memory without
// benefit of an IDE! May not be exactly correct!

// Switch the driver context to the alert
Alert alertDialog = driver.switchTo().alert();

// Get the alert text
String alertText = alertDialog.getText();

// Click the OK button on the alert.
alertDialog.accept();

关于java - 我需要使用 Java 读取 Web 驱动程序中弹出窗口中显示的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15105796/

相关文章:

java - 拆分问题(字符串正则表达式)

Java程序挂起

javascript - 咕噜声 : abstracting away some tasks

eclipse - java.lang.错误 : Unresolved compilation problems : WebDriver/ChromeDriver cannot be resolved to a type error while executing selenium tests

java - Ant + javac + WebDriver : "cannot find symbol: variable By"

Java keytool/KeyStore 没有显示 eToken 的条目

deployment - 用java更新java?

javascript - 跨域问题 - api 与前端的域不同

javascript - 拉伸(stretch) div 作为最后的手段

selenium - org.openqa.selenium.json.JsonException : Expected to read a START_MAP but instead have: END. 通过 Node 执行测试时读取的最后 0 个字符