2016-03-02 43 views
0

交互我試圖讀取包含多個「用戶名」和「密碼」的CSV文件Java中的硒。但是當我執行代碼時,我只能在CSV文件中執行第一個用戶的登錄和註銷功能。之後,它在控制檯中顯示以下錯誤: 我的實際要求是讀取csv文件中的所有數據(用戶名和密碼),並使用這些用戶名和密碼中的每一個登錄和註銷。線程「main」中的異常org.openqa.selenium.ElementNotVisibleException:元素當前不可見,因此可能不會與

*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 


    } 
    } 

在執行,以下異常顯示:用戶名: testuser01.zyx密碼:testuser0123登錄成功退出成功 用戶名:密碼testuser07:user0123異常線程 「main」 org.openqa.selenium。 ElementNotVisibleException:元素不是 當前可見,因此可能不與命令持續時間 或超時:23毫秒交互構建信息:版本:'2.52.0',修訂版本: '4c2593c',時間:'2016-02-11 19 :03:33'系統信息:host:'localhost', ip:'127.0.0.1',os.name:'Linux',os.arc h:'i386',os.version: '3.2.0-37-generic-pae',java.version:'1.7.0_95'會話ID: 52ade752-04da-4a5e-baf5-5bcdf98a2434驅動程序信息: org .openqa.selenium.firefox.FirefoxDriver功能 [{platform = LINUX,acceptSslCerts = true,javascriptEnabled = true, cssSelectorsEnabled = true,databaseEnabled = true,browserName = firefox, handlesAlerts = true,nativeEvents = false,webStorageEnabled = true, 旋轉=假,locationContextEnabled =真, applicationCacheEnabled =真,takesScreenshot =真,版本= 43.0}]在 sun.reflect.NativeConstructorAccessorImpl.newInstance0(本機方法) 在 sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAc cessorImpl.java:57) 在 sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 在java.lang.reflect.Constructor.newInstance(Constructor.java:526) 在 org.openqa.selenium。 remote.ErrorHandler.createThrowable(ErrorHandler.java:206) 在 org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158) 在 org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver。的java:678) 在 org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:327) 在 org.openqa.selenium.remote.RemoteWebElement.clear(R emoteWebElement.java:140) 在NewCsvPkg.NewCsvClass.main(NewCsvClass.java:54)產生的原因: org.openqa.selenium.ElementNotVisibleException:元素不 目前可見的,所以可能不會生成信息進行交互: 版本:'2.52.0',修訂:'4c2593c',時間:'2016-02-11 19:03:33' 系統信息: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/[email protected]/components/command-processor.js:10045) at .DelayedCommand.prototype.checkPreconditions_(file:/// tmp/anonymous8352270443321158650webdriver -profile/EXT ensions/[email protected]/components/command-processor.js:12597) at .DelayedCommand.prototype.executeInternal_/h(file:///tmp/anonymous8352270443321158650webdriver-profile/extensions/[email protected]/components/命令處理器。js:12614) at .DelayedCommand.prototype.executeInternal_(file:///tmp/anonymous8352270443321158650webdriver-profile/extensions/[email protected]/components/command-processor.js:12619) at .DelayedCommand.prototype.execute/<(文件:///tmp/anonymous8352270443321158650webdriver-profile/extensions/[email protected]/components/command-processor.js:12561)

+0

你從哪裏得到異常? – Guy

+0

@Guy:請看看我上面編輯的問題。我已指定在應用程序運行時在控制檯中顯示的內容 – Ganga

回答

1

要調用driver.close();while循環,關閉內部瀏覽器。瀏覽器關閉時不能與元素進行交互。在測試完成後將其移動到。

作爲一個側面說明,如果你想確保元素與其進行交互之前可見使用明確的等待

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

這將等待長達10秒的元素是可見的。 wait.until也正在返回它正在等待的元素。

應用實例

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); 
} 
+0

@ user254255測試完成後,在您正好在'System.exit(0);'之前的情況下。 – Guy

+0

我已經放置了driver.close();在System.exit(0)之前,但仍未執行CSV文件中第二個用戶的登錄功能。它仍然顯示與以前相同的錯誤消息: – Ganga

+0

@ user254255您是否嘗試過將顯式等待添加到我的答案中? – Guy

0

我的朋友試試這個:

  • 把所有相關的內部時硒驅動程序代碼。這包括你的Driver對象的聲明。

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

讓我知道?

+0

非常感謝..它爲我工作.. – Ganga

+0

@ pjcalvo:我現在有一個問題。當上述提到的行包含在While循環中時,每次在執行時都會打開新的瀏覽器以進行新的登錄。我怎樣才能在單個瀏覽器中完成所有登錄? – Ganga

+0

@Ganga對不起,以前沒有回答。您只需要移動 WebDriver驅動程序= new FirefoxDriver(); – pjcalvo

0

我喜歡Guy's和pjcalvo答案,但我首先會建議的是簡化問題。對我來說,問題是我不知道這是硒還是opencsv問題(或兩者兼而有之)。

我會在這種情況下做的是把你的原始程序,並將其分爲兩個測試程序。第一個是通過csv文件的while循環,並在屏幕上輸出用戶名和密碼。第二個是處理硬編碼的二維字符串數組名稱和密碼的硒測試(比如說三個用戶,因爲現在你沒有越過第一個用戶)。這兩個程序都使用與上面列出的程序相同的樣式。

這樣做會讓您更好地瞭解什麼是真正的問題,以及更小的程序示例來調試或提出問題。一旦較小的程序正在運行,那麼你可以修復真正的程序。

:)

相關問題