[英]Java Selenium - Unable to reach to Alert with both Chrome and firefox
我是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:在45000毫秒后无法连接到端口7055上的主机127.0.0.1。 Firefox控制台输出。
在使用Chrome驱动程序运行时,出现以下异常:
线程“主要” org.openqa.selenium.WebDriverException中的异常:未知错误:无法从意外警报打开中确定加载状态(会话信息:chrome = 53.0.2785.101)(驱动程序信息:chromedriver = 2.21.371459 ...
任何帮助将是可观的。
对于Firefox来说,Selenium的Webdriver版本似乎有问题,而对于Chrome,则是Chromedriver版本。 下载并应用了硒的最新Beta版本和铬的最新chromedriver。 现在工作正常。
要启动最新的Mozilla Firefox ,您需要下载最新的geckodriver
可执行文件 ,然后按照以下答案进行操作,即如何使用硒启动最新的Firefox 。
线程“主要” org.openqa.selenium.WebDriverException中的异常:未知错误:无法从意外警报打开中确定加载状态
其实没有必要切换Alert
两次获得警报文本,然后接受。 您可以使用一次切换到Alert
来完成这两个任务。
为了更好的方法,您应该尝试使用WebDriverWait
切换Alert
以等待直到警报出现,而不是如下所示的Thread.sleep()
:
driver.findElement(By.cssSelector("input[value='Go!']")).click();
WebDriverWait wait = WebDriverWait(driver, 10);
Alert alert = wait.until(ExpectedConditions.alertIsPresent());
String S = alert.getText();
System.out.println(S);
alert.accept();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.