0

I'm trying to read a CSV file containing more than one 'username' and 'password' using java in selenium . But when I execute the code, I'm only able to execute the login and logout functionality of the first user in the CSV file. After that it is showing the following error in the console: My actual requirement is to read all the datas (username and password) in the csv file and login and logout using each one of these usernames and passwords.

*Exception in thread "main" org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with*

Following is my entire code: 


package 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;

//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  {

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

            //start


            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);
        System.out.println("Login Success");

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

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

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

             //catch exception
             catch(Exception e)
             {
                 System.out.println("Login failed!");  
             }// catch closed
//           //closing driver & firefox
             driver.close();
            //end



        }//while end

     //csv reader end
        System.exit(0);       //closing firefox  


  } 
  }

While executing, following exception is shown: Username: testuser01.zyx Password: testuser0123 Login Success Logout Success Username: testuser07 Password: user0123 Exception in thread "main" org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with Command duration or timeout: 23 milliseconds Build info: version: '2.52.0', revision: '4c2593c', time: '2016-02-11 19:03:33' System info: host: 'localhost', ip: '127.0.0.1', os.name: 'Linux', os.arch: 'i386', os.version: '3.2.0-37-generic-pae', java.version: '1.7.0_95' Session ID: 52ade752-04da-4a5e-baf5-5bcdf98a2434 Driver info: org.openqa.selenium.firefox.FirefoxDriver Capabilities [{platform=LINUX, acceptSslCerts=true, javascriptEnabled=true, cssSelectorsEnabled=true, databaseEnabled=true, browserName=firefox, handlesAlerts=true, nativeEvents=false, webStorageEnabled=true, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=43.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) at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206) at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678) at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:327) at org.openqa.selenium.remote.RemoteWebElement.clear(RemoteWebElement.java:140) at NewCsvPkg.NewCsvClass.main(NewCsvClass.java:54) Caused by: org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with Build info: version: '2.52.0', revision: '4c2593c', time: '2016-02-11 19:03:33' System info: host: 'localhost', ip: '127.0.0.1', os.name: 'Linux', os.arch: 'i386', os.version: '3.2.0-37-generic-pae', java.version: '1.7.0_95' Driver info: driver.version: unknown at .fxdriver.preconditions.visible(file:///tmp/anonymous8352270443321158650webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:10045) at .DelayedCommand.prototype.checkPreconditions_(file:///tmp/anonymous8352270443321158650webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12597) at .DelayedCommand.prototype.executeInternal_/h(file:///tmp/anonymous8352270443321158650webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12614) at .DelayedCommand.prototype.executeInternal_(file:///tmp/anonymous8352270443321158650webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12619) at .DelayedCommand.prototype.execute/<(file:///tmp/anonymous8352270443321158650webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12561)

Guy
  • 46,488
  • 10
  • 44
  • 88
Ganga
  • 17
  • 5

3 Answers3

1

You are calling driver.close(); inside the while loop, which closes the browser. You can't interact with elements when the browser is closed. Move it to after the test was completed.

As a side note, if you want to make sure the element is visible before interacting with it use explicit wait

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement username = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("Email")));
username.sendKeys(user_name);

This will wait up to 10 seconds for the element to be visible. wait.until is also returning the element it was waiting for.

Uses example

WebDriverWait wait = new WebDriverWait(driver, 10); // initialize wait object

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

    // locate and write to user name
    WebElement username = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("Email")));
    username.clear();
    username.sendKeys(user_name);

    // locate and write to password
    WebElement password = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("Passwd")));
    password.clear();
    password.sendKeys(user_name);
}
Guy
  • 46,488
  • 10
  • 44
  • 88
  • @user254255 After the test was completed, in you case just before `System.exit(0);`. – Guy Mar 02 '16 at 11:58
  • I have placed the driver.close(); before System.exit(0), but still its not executing the login functionality of the second user in the CSV file. Its still showing the same error message as previous: – Ganga Mar 02 '16 at 12:04
  • @user254255 did you try the explicit wait I added to my answer? – Guy Mar 02 '16 at 12:07
  • @user254255 I added example with `username` and 'password`. You can use it with other webelements as well. – Guy Mar 02 '16 at 12:20
0

My friend try this:

  • Put all the code related to the selenium driver inside the While. That includes your declaration of the Driver object.

    while ((nextLine = reader.readNext())!= null){        
    WebDriver driver = new FirefoxDriver();    
             String appUrl = "https://accounts.google.com";
             driver.get(appUrl);
             driver.manage().window().maximize();   
    driver.Close();}
    

And let me know?

  • @ pjcalvo: I have got an issue here now. When the above mentioned lines are included inside the While loop, each time a new browser is opened for new login while executing. How can I do all the logins in a single browser? – Ganga Mar 03 '16 at 07:05
  • @Ganga I am sorry for not answering before. You just needed to move the WebDriver driver = new FirefoxDriver(); – pjcalvo Nov 08 '16 at 17:22
  • @Ganga I am sorry for not answering before. You just needed to move the WebDriver driver = new FirefoxDriver(); and the Driver.Close both outside of the While and that would work – pjcalvo Nov 08 '16 at 17:22
0

I like both Guy's and pjcalvo answers but what I would suggest first is to simplify the problem. To me the issue is that I do not know if this is a selenium or opencsv issue (or both).

What I would do in this case is take your original program and break it into two test programs. The first would be a while loop through the csv file and print out the user names and passwords on the screen. The second would be a selenium test dealing with a hard coded two dimensional string array of names and passwords (say three users because right now you are not getting past the first one). Both of these programs using the same style as the program you listed above.

Doing this will give you a better idea of what is the real issue and a smaller program example to debug or ask questions about. Once the smaller program is running then you can take the fix to the real program.

:)

Scott Conway
  • 975
  • 7
  • 13