有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我正在尝试理解和练习阅读和Excel文件,并将登录凭证发送到网页。在测试excel文件中,我有2个用户名和2个密码。第一组工作正常(即发送到网页字段),但在第二组中,我遇到以下问题:

线程“main”组织中出现异常。openqa。硒。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();


    }

}

我试过这条线。睡吧,司机。quit()这些修改没有任何影响

让我知道我还能尝试什么,谢谢你的时间


共 (1) 个答案

  1. # 1 楼答案

    第一次迭代后,浏览器窗口似乎关闭。。。在运行时观察您的测试用例。。。请参见它在第一次迭代后关闭浏览器窗口,即driver.close();。。删除此行并在for循环完成后放置位置,可能会对您有所帮助。e、 g

    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();