Java Selenium - 无法使用 Chrome 和 Firefox 访问警报

标签 java selenium selenium-webdriver alert selenium-firefoxdriver

我是Selenium的新手,我编写了以下代码来打印警报文本并接受警报。

public class AlertPractice {

 public static void main(String[] args) throws InterruptedException {

    WebDriver driver  = new FirefoxDriver();
    driver.get("http://output.jsbin.com/usidix/1");

    driver.findElement(By.cssSelector("input[value=\"Go!\"]")).click();
    Thread.sleep(1000);
    String S = driver.switchTo().alert().getText();
    Thread.sleep(1000);
    driver.switchTo().alert().accept();

    System.out.println(S);
    driver.close();
}

使用 FIREFOX 驱动程序运行时,出现以下异常:

org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms. Firefox console output....

在使用 Chrome 驱动程序运行时,我遇到以下异常:

Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: cannot determine loading status from unexpected alert open (Session info: chrome=53.0.2785.101) (Driver info: chromedriver=2.21.371459 ...

任何帮助将不胜感激。

最佳答案

对于 Firefox 和 Chrome 的 Chromedriver 版本,Selenium 的 Webdriver 版本似乎存在问题。下载并应用了 selenium 的最新 beta 版本和 chrome 的最新 chromedriver。现在工作正常。

关于Java Selenium - 无法使用 Chrome 和 Firefox 访问警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39427529/

相关文章:

java - 具有静态内容和 REST 资源的 Grizzly 服务器

java - 错位的构造

java - 使用三元运算符在 spring 配置中传递参数

java - 选择菜单时出错

c# - 正确使用 phantomJS 和代理

java - 正则表达式将数字与某物或无匹配

Python 和 Selenium : Difference between driver.implicitly_wait() 和 time.sleep()

selenium - Java/JUnit 和 Selenium RC - 是否有包装库?

Selenium 获取当前隐式等待的值

selenium - 哪个 Firefox 版本与 Selenium 3.6.0 兼容