我试图关闭Webdriver上的浏览器实例但无法这样做

时间:2015-03-17 06:18:47

标签: java eclipse selenium

package erewards2;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class login_sib2 {

    public static void main(String[] args) {

        FirefoxDriver d1 = new FirefoxDriver();

                 d1.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
                 d1.get("https://sib3.erewardsredeem.com/fm/customer.html?action=userLogin");
                 d1.manage().window().maximize();
                 WebElement e4 = d1.findElementByLinkText("Login");
                 e4.click();
                 d1.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
                 WebElement e1 = d1.findElement(By.id("showmemberid"));
                 e1.sendKeys("2800000091");
                 WebElement e2 = d1.findElementById("showmemberpwd");
                 e2.sendKeys("Miquser1");
                 WebElement e3 =  d1.findElementByClassName("loginbtn");
                 e3.click();

                WebElement e5 = d1.findElementByLinkText("Logout");

                 e5.click();



                Alert a1 = d1.switchTo().alert(); //web based alert
                WebDriverWait wait = new WebDriverWait(d1,10);
                wait.until(ExpectedConditions.alertIsPresent()).accept();
                a1.accept();

             System.out.println("test1");

                //close Firefox
                d1.close();
                System.out.println("test2");   
                // exit the program explicitly
                System.exit(0);
                System.out.println("test3");
    }

}

我添加了system.out println行以进行测试,发现我的代码无法从Test1语句中完全执行。以下是我在Eclipse IDE上的例外情况。

  

线程“main”中的异常org.openqa.selenium.NoAlertPresentException:不存在警报(警告:服务器未提供任何堆栈跟踪信息)   命令持续时间或超时:13.82秒   构建信息:版本:'2.41.0',修订版:'3192d8a',时间:'2014-03-27 17:18:15'   系统信息:主机:'pcs-PC',ip:'192.168.1.64',os.name:'Windows 7',os.arch:'x86',os.version:'6.1',java.version:'1.7 .0_25'   会议ID:59fe366a-5fdc-4763-a6f2-0860bf1378be   驱动程序信息:org.openqa.selenium.firefox.FirefoxDriver   Capabilities [{platform = XP,acceptSslCerts = true,javascriptEnabled = true,cssSelectorsEnabled = true,databaseEnabled = true,browserName = firefox,handlesAlerts = true,browserConnectionEnabled = true,webStorageEnabled = true,nativeEvents = false,rotating = false,locationContextEnabled = true ,applicationCacheEnabled = true,takeScreenshot = true,version = 30.0}]       at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)       at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)       at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)       at java.lang.reflect.Constructor.newInstance(Constructor.java:526)       在org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:193)       在org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)       在org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:595)       在org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:610)       在org.openqa.selenium.remote.RemoteWebDriver $ RemoteAlert.accept(RemoteWebDriver.java:910)       在erewards2.login_sib2.main(login_sib2.java:40)

2 个答案:

答案 0 :(得分:5)

它看起来在它到达close语句之前就失败了。

尝试像这样构建代码:

FirefoxDriver d1 = new FirefoxDriver();
try {
    // steps
}
finally {
    d1.close();
}

这将保证执行close语句。

答案 1 :(得分:1)

您也可以尝试使用d1.quit()。