0

Query - The last set of values keep failing, the error that i am getting is that its unable to fine the xpath, but its working fine for other two cases. So a bit confused. I have pasted the error code as well. Would appreciate any help I can get. Thanks in advance.

Test Code -

@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] = "blah@blah.com";
                data[2][1] = "kingkongkijai";

                return data;

            }
        }

Error -

FAILED: loginScenario("blah@blah.com", "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

  • Could you give us the website where you are trying to test? Currently i think one of the reason why it fails is because there is no timeout set for the driver. So it times out before finding the element. – Madis Kangro Jul 04 '17 at 07:22
  • @Madis Kangro - Thank you for your response. Would like to share that the 2 out of three sets run fine and also since the last set is valid username and password it also logs in in the Firefox instance started by driver. However it shows fail in console. I am sorry I cant give you the details of the URL. I would add some timeout and update you shortly. – Avinash Pandey Jul 04 '17 at 07:30
  • Also a question. Should it be a success case with the final set of data? If yes, then the issue is that it logs into the website and then this driver.findElement(By.xpath(".//*[@id='LoginForm_username']")).clear(); element will disappear from the view, which leads to element not found exception. – Madis Kangro Jul 04 '17 at 07:37
  • @MadisKangro Yes, it shoud be a success. I am sure you are right. However how do i remove this, driver.findElement(By.xpath(".//*[@id='LoginForm_username']"‌​)).clear(); dependency after success. Thank so much bro – Avinash Pandey Jul 04 '17 at 23:42
  • 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(); } – Avinash Pandey Jul 05 '17 at 00:38
  • The above did the trick thanks so much for pointing out and helping till the end :) much appriciated. – Avinash Pandey Jul 05 '17 at 00:39
  • a simple way to get rid of the dependency would be to add the clear before typing the text. So you don't add unnecessary code. So first you clear. Then you send keys and then you click. – Madis Kangro Jul 05 '17 at 06:37

1 Answers1

0

@Madis Kangro

I just added the below code after you inputs and you were bang on ! Thanks so much

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