如何使用selenium从excel读取多个用户名和密码

时间:2017-09-17 09:58:54

标签: selenium selenium-webdriver

public static void main(String args[]) throws IOException, JXLException, BiffException, FileNotFoundException, InterruptedException { 

    System.setProperty("webdriver.chrome.driver","C:\\Users\\yirsh\\Desktop\\Selenium\\chromedriver_win32\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();

    Sheet s;

    FileInputStream fi = new FileInputStream("C:\\Users\\yirsh\\Desktop\\UAT WEBSHOP.xls");
    Workbook w = Workbook.getWorkbook(fi);
    s = w.getSheet(1);
    int z = s.getRows();
    System.out.println("no of Users:"+z);
    driver.get("http://...@...ip.../path/");
    driver.manage().window().maximize();
    //Thread.sleep(10000);
    for(int row = 1; row < 6; row++)
    {
        String username = s.getCell(2, 1).getContents();
        System.out.println("Username: "+username);
        driver.findElement(By.id("j_username_leftAside")).sendKeys(username);

        String password = s.getCell(3, 1).getContents();
        System.out.println("Password: "+password);
        driver.findElement(By.id("j_password_leftAside")).sendKeys(password);

        driver.findElement(By.xpath("/html[@class=' js opacity generatedcontent pointerevents']/body[@class='page-homepage pageType-ContentPage template-pages-layout-RexelHomePageLayout pageLabel-homepage language-de ']/div[@id='page']/div[@id='content']/div[@id='content']/div[@class='content-top-inner']/div[@id='content-inner']/div[@class='mid-wrapper'][1]/div[@class='yCmsContentSlot']/div[@class='login clear']/form[@id='loginForm']/div[@class='left sign-in']/button[@class='Sign-in rx-btn mb0']")).click();

        if(driver.getPageSource().contains("Willkommen"))
        {
            System.out.println("Login Sucessfull");     
        }
        else
        {
            System.out.println("Login Failed");
        }

        driver.quit();
    }
}

我有超过10个用户凭据,但代码只读取一个用户凭据并退出请根据我的需要更正代码。因为我是新编码有点困惑的(int row = 1; row < 6; row++)纠正我的确切代码

2 个答案:

答案 0 :(得分:1)

如果登录成功,则需要编写注销码(我已发表评论)。此外,您需要使用行varibale。另外,将driver.quit()放在外面循环。

public static void main(String args[]) throws IOException, JXLException,BiffException,FileNotFoundException, InterruptedException{ 
    System.setProperty("webdriver.chrome.driver","C:\\Users\\yirsh\\Desktop\\Selenium\\chromedriver_win32\\chromedriver.exe");
    WebDriver driver=new ChromeDriver();

    Sheet s;

    FileInputStream fi = new FileInputStream("C:\\Users\\yirsh\\Desktop\\UAT WEBSHOP.xls");
    Workbook w = Workbook.getWorkbook(fi);
    s = w.getSheet(1);
    int z=s.getRows();
    System.out.println("no of Users:"+z);
    driver.get("http://RexelQAEnv:Rexel!23@52.19.191.249/aus/");
    driver.manage().window().maximize();
    //Thread.sleep(10000);

    for(int row=1; row <= 10; row++)
    {

        String username = s.getCell(2,row).getContents();
        System.out.println("Username: "+username);
        driver.findElement(By.id("j_username_leftAside")).sendKeys(username);

        String password= s.getCell(3,row).getContents();
        System.out.println("Password: "+password);
        driver.findElement(By.id("j_password_leftAside")).sendKeys(password);

        driver.findElement(By.xpath("/html[@class=' js opacity generatedcontent pointerevents']/body[@class='page-homepage pageType-ContentPage template-pages-layout-RexelHomePageLayout pageLabel-homepage language-de ']/div[@id='page']/div[@id='content']/div[@id='content']/div[@class='content-top-inner']/div[@id='content-inner']/div[@class='mid-wrapper'][1]/div[@class='yCmsContentSlot']/div[@class='login clear']/form[@id='loginForm']/div[@class='left sign-in']/button[@class='Sign-in rx-btn mb0']")).click();

        if(driver.getPageSource().contains("Willkommen"))
        {
            System.out.println("Login Sucessfull");
            // write code for logout
        }
        else
        {
        System.out.println("Login Failed");
        }


     }
      driver.quit();
    }

答案 1 :(得分:0)

我的猜测是,尽管你正在增加一个row变量,但你不应该在你应该的地方使用它:

Sheet s;
// ...
for(int row=1; row < 6; row++) {
    String username = s.getCell(2,1).getContents();

假设sjxl.SheetgetCell method的第二个参数是您感兴趣的行号。

所以你可能想在那里打电话s.getCell(2, row),因为你的row变量中有行号。