2

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 = "http://10.10.80.11/";
        driver.get(baseURL);


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

            // Enter user id
            element.sendKeys("abc");

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

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

            String expectedURL = "http://10.10.80.11/";
            String actualURL;
            actualURL = driver.getCurrentUrl();

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

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);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("user")));

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

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

public class LoginPage
{
static WebDriver driver;

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


@BeforeClass
public static void setup() throws TestNGException, IOException, WriteException
{
    System.setProperty("webdriver.chrome.driver", "E:\\Selenium Jar\\chromedriver.exe");
    ChromeOptions options = new ChromeOptions();
    options.addArguments("--disable-extensions");
    driver = new ChromeDriver(options);
    driver.manage().window().maximize();
}

@Test
public void data()
{

    driver.get("http://abc.com");

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

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

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

    String actual = driver.getCurrentUrl();

     Assert.assertEquals(actual,expectedurl);

}

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

1

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
1

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.