0

查詢 - 最後一組值不斷失敗,我得到的錯誤是它無法罰款xpath,但其工作正常的其他兩種情況。所以有點困惑。我也粘貼了錯誤代碼。希望我能得到任何幫助。提前致謝。Dataprovider anotation - Testng Selenium Java

測試代碼 -

@Test(priority = 4, dataProvider = "getData") 
      public void loginScenario(String Username, String Password) throws InterruptedException { 

WebElement user = driver.findElement(By.xpath(".//*[@id='LoginForm_username']")); 
       if(null != user) { 
        user.clear(); 
       } 

       WebElement pass = driver.findElement(By.xpath(".//*[@id='LoginForm_password']")); 
       if(null != pass) { 
        pass.clear(); 
       } 

       // driver.findElement(By.xpath(".//*[@id='LoginForm_username']")).click(); 
       // driver.findElement(By.xpath(".//*[@id='LoginForm_username']")).click(); 
       driver.findElement(By.xpath(".//*[@id='LoginForm_username']")).sendKeys(Username); 
       driver.findElement(By.xpath(".//*[@id='LoginForm_password']")).sendKeys(Password); 
       driver.findElement(By.xpath(".//*[@id='login-form']/button")).click(); 
       driver.findElement(By.xpath(".//*[@id='LoginForm_username']")).clear(); 
       driver.findElement(By.xpath(".//*[@id='LoginForm_password']")).clear(); 

      } 

      @DataProvider 
      public Object[][] getData() { 

       Object[][] data = new Object[3][2]; 
       data[0][0] = "abcd"; 
       data[0][1] = "xyz"; 
       data[1][0] = "..."; 
       data[1][1] = "."; 
       data[2][0] = "[email protected]"; 
       data[2][1] = "kingkongkijai"; 

       return data; 

      } 
     } 

Error - 

FAILED: loginScenario("[email protected]", "kingkongkijai") 
org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":".//*[@id='LoginForm_username']"} 
Command duration or timeout: 51 milliseconds 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423) 
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:215) 
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:167) 
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:671) 
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:410) 
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:509) 
    at org.openqa.selenium.By$ByXPath.findElement(By.java:361) 
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:402) 
    at pages.LoginPage.loginScenario(LoginPage.java:69) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108) 
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:661) 
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:869) 
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1193) 
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126) 
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109) 
    at org.testng.TestRunner.privateRun(TestRunner.java:744) 
    at org.testng.TestRunner.run(TestRunner.java:602) 
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:380) 
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375) 
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340) 
    at org.testng.SuiteRunner.run(SuiteRunner.java:289) 
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) 
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) 
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301) 
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1226) 
    at org.testng.TestNG.runSuites(TestNG.java:1144) 
    at org.testng.TestNG.run(TestNG.java:1115) 
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132) 
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230) 
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76) 
Caused by: org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":".//*[@id='LoginForm_username']"} 
For documentation on this error, please visit: 

http://seleniumhq.org/exceptions/no_such_element.html

+0

你可以給我們你想要測試的網站嗎?目前我認爲它失敗的原因之一是因爲沒有爲駕駛員設置超時。所以它在找到元素之前就超時了。 –

+0

@Madis Kangro - 感謝您的回覆。想分享一下,三組中的兩組運行正常,而且自從最後一組設置有效的用戶名和密碼之後,它也會登錄到由驅動程序啓動的Firefox實例中。但是它在控制檯中顯示失敗。對不起,我不能給你的網址的詳細信息。我會添加一些超時並儘快更新。 –

+0

也是一個問題。最終的數據集應該是成功案例嗎?如果是的話,那麼問題是它登錄到網站,然後這個driver.findElement(By.xpath(「.//* [@ id ='LoginForm_username']」))。元素將從視圖中消失,這導致元素未找到異常。 –

回答

0

@Madis Kangro

我剛添加下面的代碼,你輸入後,你是在砰!非常感謝

WebElement user = driver.findElement(By.xpath(".//*[@id='LoginForm_username']"‌​)); if(null != user) { user.clear(); } WebElement pass = driver.findElement(By.xpath(".//*[@id='LoginForm_password']"‌​)); if(null != pass) { pass.clear(); }