I'm getting an Exception as below ,when i run the selenium code by using a loop. It executes some steps and then display the message.

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: *[name='user']

My code is:

package login;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class login {

    public static void main(String[] args) {

        // Web Driver configuration
        System.setProperty("webdriver.gecko.driver", "E:\\Software\\geckodriver-v0.11.1-win64\\geckodriver.exe");

        WebDriver driver = new FirefoxDriver  ();
        String baseURL = "";

        // Login_logout Process     
        for (int i=1;i<=100;i++){
            WebElement element = driver.findElement(By.name("user"));                            

            // Enter user id

            WebElement element1 = driver.findElement(By.name("password")); 

            // Enter user id
            driver.findElement(By.id("button-1013-btnEl")).click(); //Submit button
            driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
            driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

            String expectedURL = "";
            String actualURL;
            actualURL = driver.getCurrentUrl();

            //compare the actual URL of the page with the expected one and print
            if (actualURL.contentEquals(expectedURL)){
            else {   
                System.out.println("Test Fail!");
  • It seems you skipped logout step before next login.. – prinz Jan 4 '17 at 4:33

Always use the explicit wait to let the webelement get loaded before performing any execution on it. A very common method used for the wait is as follows.

/* Initialize the WebDriverWait, with 30 seconds of wait time. */
WebDriverWait wait = new WebDriverWait(driver, 30);

Add this code before the line where you are getting the error.

import org.testng.Assert; // Importing Assert class from TestNG library

public class LoginPage
static WebDriver driver;

String  expectedurl = "http://example.com";

public static void setup() throws TestNGException, IOException, WriteException
    System.setProperty("webdriver.chrome.driver", "E:\\Selenium Jar\\chromedriver.exe");
    ChromeOptions options = new ChromeOptions();
    driver = new ChromeDriver(options);

public void data()


    WebElement username = driver.findElement(By.name("log"));

    WebElement pass = driver.findElement(By.name("pwd"));

    WebElement submit = driver.findElement(By.name("Submit"));

    String actual = driver.getCurrentUrl();



Please Use Method for the different condition .do not use loop for that and use annotation for the selenium please read this link about annotation https://www.tutorialspoint.com/testng/testng_basic_annotations.htm


I'm getting an Exception as below ,when i run the selenium code by using a loop

  • Agree with Keshav, You missed to "logout" before next login attempt.

  • Add or implement logout step to be back on login screen

I have few suggestions:

1. Avoid use of looping :

  • You must be aware of the execution in automation is little depend of previous line of code

  • If any line not executed or failed to execute we have limitations to handle that [We can do it for some extend but not for all line 100%]

  • It would be disadvantageous to use loop as if any thing get failed you will get false alarm

2. Use more & more functions :

  • In your code you have written all your statements in one class

  • To make things re-usable and minimized failures you can think of using small functions for actions

  • Example: One function will accept username & password which will actual enter credentials and tap on login button. Second function for logout etc.

3. Instead of loop, You can inject test data from xls and simple increase number of iterations

  • This will be most efficient way to implement without using loops

As per your code, you have already logged in that's why in second loop you can't find the By.name("user") element. Before ending the loop logout from the application and just after starting the loop re-load the base url using code driver.get(baseURL);, so that you can come to the original page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.