繁体   English   中英

需要通过使用Selenium中的Java从CSV文件中读取邮件ID和密码来在同一浏览器窗口中打开每个登录名

[英]Need to open each login in the same browser window by reading mailids and passwords from a CSV file using Java in Selenium

我正在尝试在Selenium中使用java读取包含邮件ID和密码的CSV文件,读取后,我需要在同一浏览器中登录到每个邮件ID。 我已经为此编写了整个代码,但问题是它正在不同的浏览器窗口中打开每个用户登录。 我的要求是在同一浏览器窗口中打开每个用户登录。 我正在使用Firefox浏览器。 下面是我的完整代码:包NewCsvPkg;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
//import org.openqa.selenium.support.ui.ExpectedConditions;
//import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.WebDriverWait;

//csv reader imports
import java.io.FileReader;
import java.util.Iterator;
import java.util.List;
//import au.com.bytecode.opencsv.CSVReader;
import com.opencsv.CSVReader;
public class NewCsvClass {

  public static void main(String[] args) throws Exception  {

      //put this 4 lines inside while loop.

//         WebDriver driver = new FirefoxDriver();   
//       String appUrl = "https://accounts.google.com";
//       driver.get(appUrl);
//       driver.manage().window().maximize(); 



     //csv reader aswathy -start
        CSVReader reader = new CSVReader(new FileReader("/home/user/Documents/UrmilaDocs/CSV PAck/testCSV.csv"));
        String [] nextLine;

        while ((nextLine = reader.readNext())!= null){

        String user_name = nextLine[0];
        String pass_word = nextLine[1];

        System.out.println("Username: " + user_name);
        System.out.println("Password: " + pass_word);


    //stackoverflow     
        WebDriver driver = new FirefoxDriver();  
         String appUrl = "https://accounts.google.com";
         driver.get(appUrl);
         driver.manage().window().maximize();   


        WebElement username = driver.findElement(By.xpath(".//*[@id='Email']"));
        username.clear();
        username.sendKeys(user_name);
        driver.findElement(By.xpath(".//*[@id='next']")).click();

        Thread.sleep(5000);
             //try
             try{
            WebElement password = driver.findElement(By.xpath(".//*[@id='Passwd']"));
              password.clear();
              password.sendKeys( pass_word);
              driver.findElement(By.xpath(".//*[@id='signIn']")).click();
        Thread.sleep(8000);

        //click on 'Google Apps' icon 
        driver.findElement(By.xpath(".//*[@id='gbwa']/div[1]/a")).click();
        Thread.sleep(10000);

        //Click on 'Gmail' icon to navigate to inbox page
        driver.findElement(By.xpath(".//*[@id='gb23']/span[1]")).click();
        Thread.sleep(10000);
        System.out.println("Login Success");


        //Click on user name first letter circle icon
        driver.findElement(By.xpath(".//*[@id='gb']/div[1]/div[1]/div[2]/div[4]/div[1]/a/span")).click();
        Thread.sleep(5000);

        //click on 'Signout' button
        driver.findElement(By.xpath(".//*[@id='gb_71']")).click();
        Thread.sleep(10000);
        System.out.println("Logout Success");

        /*click on 'Signin with a different account ' option (since, after signing out from
//      the first user, he page is navigated to password entry page, which is supposed to navigate to 
//      username/mailid entry page  */
        driver.findElement(By.xpath(".//*[@id='account-chooser-link']")).click();
        Thread.sleep(10000);
//      /* In 'Choose an Account page', Click on 'Add Account' button */
        driver.findElement(By.xpath(".//*[@id='account-chooser-add-account']")).click();
        Thread.sleep(10000);
//          
    }catch(Exception e)
             {
                 System.out.println("Login failed!");  
             }// catch closed
//           //closing driver & firefox
             //driver.close();
             //System.exit(0);
            //end
        }   //while end

//      //closing driver & firefox
        // driver.close();
//     //csv reader aswathy -end
        System.exit(0);       //closing firefox  

  } 


  }
    You should initialise driver before entering into loop, currently you are initialising driver in loop thus causing driver to open multiple windows, also close firefox only after completing loop else you may face problem in next iteration of loop.

    Change it like this:

     //Initialise driver here and then go to loop:
            WebDriver driver = new FirefoxDriver();  
             String appUrl = "https://accounts.google.com";
             driver.get(appUrl);
             driver.manage().window().maximize();   


     while ((nextLine = reader.readNext())!= null){

            String user_name = nextLine[0];
            String pass_word = nextLine[1];

            System.out.println("Username: " + user_name);
            System.out.println("Password: " + pass_word);


    }

   System.exit(0); 

暂无
暂无

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

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