繁体   English   中英

从Excel获取时读取Selenium Java-没有此类窗口异常

[英]Selenium Java while reading from Excel getting-No such window exception

我正在尝试理解和练习从Excel文件读取并将登录凭据发送到网页。 在测试excel文件中,我有2个用户名和2个密码。 第一组有效(意味着发送到网页字段),但是在第二组期间出现以下问题:

线程“主”中的异常org.openqa.selenium.NoSuchWindowException:找不到窗口。 浏览器窗口可能已关闭。 命令持续时间或超时:221毫秒

这是代码:

public class EX1 {

    public static WebDriver driver;

    public int cellNo;

    public WebDriver utility(String browser, String url){
        if(browser.equals("ff")){
            driver= new FirefoxDriver();
        }
        driver.get(url);
        return driver;
    }
    public ArrayList<String> readExcel(int cellNo) throws IOException{
        FileInputStream fis = new FileInputStream("C:\\dev\\Petco.xls");
        HSSFWorkbook workbook = new HSSFWorkbook(fis);
        HSSFSheet sheet = workbook.getSheetAt(0);

        Iterator<Row> rowIterator= sheet.iterator();

        rowIterator.next();// Escapes the Header

        ArrayList<String> list= new ArrayList<String>(); //After reading the data I will add them to String list

        while(rowIterator.hasNext()){

            //list.add(rowIterator.next().getCell(0).getStringCellValue());//will just print 1st username
            //list.add(rowIterator.next().getCell(1).getStringCellValue()); // will just print 1st password
            //The above code with hardcoded 0,1- I can't call them in below getPage function. I will
            //make a parameter for this method which I will pass, cellNo is a parameter instead of 0,1           

            list.add(rowIterator.next().getCell(cellNo).getStringCellValue());//will just print 1st username
        }
        return list;

    }

    public void loginPage(String uname,String psd){
        WebElement username=driver.findElement(By.xpath("//input[@id='WC_AccountDisplay_FormInput_logonId_In_Logon_1']"));
        WebElement password=driver.findElement(By.xpath("//input[@id='WC_AccountDisplay_FormInput_logonPassword_In_Logon_1']"));
        WebElement login_btn=driver.findElement(By.xpath("//button[contains(@id,'WC_AccountDisplay_links_2')]"));

        username.sendKeys(uname);
        password.sendKeys(psd);
        login_btn.click();
    }
    public void getPage() throws InterruptedException, IOException{
        EX1 e= new EX1();
        e.utility("ff", "https://www.petco.com/shop/AjaxLogonForm?catalogId=10051&myAccountActivePage=myAccount&myAcctMain=1&langId=-1&storeId=10151");
        EX1 e1=PageFactory.initElements(driver, EX1.class);

        //Will use the list from above that contains the login information here
        ArrayList<String> username=e.readExcel(0); //-- reading the 1st column which has user name
        ArrayList<String> password=e.readExcel(1); //-- reading the 1st column which has user name
        for(int i=0; i<username.size(); i++){  //-- I want to read excel multiple times which is why the for loop
            e1.loginPage(username.get(i), password.get(i));
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
            driver.close();
        }

    }


    public static void main(String[] args) throws  IOException, InterruptedException  {
        EX1 e= new EX1();
        e.getPage();
        //e.readExcel();


    }

}

我尝试过Thread.sleep,driver.quit()那些修改没有任何影响。

让我知道我还能尝试什么,谢谢您的宝贵时间。

错误似乎浏览器窗口在第一次迭代后关闭了...在运行时观察您的测试用例...看到它在第一次迭代后正在关闭浏览器窗口,即driver.close(); ..删除该行并在循环完成后放到某个地方可能会有所帮助。 例如

for(int i=0; i<username.size(); i++){  //-- I want to read excel multiple times which is why the for loop
            e1.loginPage(username.get(i), password.get(i));
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        }
    driver.close();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM