我是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/