1

I am writing a code that enters the username and password into a URL and submits that page. but I keep getting this error

 "Caused by: org.openqa.selenium.remote.ErrorHandler$UnknownServerException: 
  Unable to locate element: {"method":"name","selector":"username"}"

Below is the code

package org.openqa.selenium.example;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class LoginPage {
    private final WebDriver driver;

    public LoginPage(WebDriver driver) {
        this.driver = driver;

        // Check that we're on the right page.
        if (!"Outreach Configuration".equals(driver.getTitle())) {
            // Alternatively, we could navigate to the login page, perhaps logging out first
            throw new IllegalStateException("This is not the login page");
        }
    }

    // The login page contains several HTML elements that will be represented as WebElements.
    // The locators for these elements should only be defined once.

    //    By usernameLocator = By.name("username");
    //    By passwordLocator = By.name("password");
     //   By loginButtonLocator = By.name("submit");

    // The login page allows the user to type their username into the username field
    public LoginPage typeUsername(String username) {
        // This is the only place that "knows" how to enter a username
        driver.findElement(By.name("username")).sendKeys(username);

        // Return the current page object as this action doesn't navigate to a page represented by another PageObject
        return this;    
    }

    // The login page allows the user to type their password into the password field
    public LoginPage typePassword(String password) {
        // This is the only place that "knows" how to enter a password
        //driver.findElement(passwordLocator).sendKeys(password);
        driver.findElement(By.name("password")).sendKeys(password);
        // Return the current page object as this action doesn't navigate to a page represented by another PageObject
        return this;    
    }

    // The login page allows the user to submit the login form
    public HomePage submitLogin() {
        // This is the only place that submits the login form and expects the destination to be the home page.
        // A seperate method should be created for the instance of clicking login whilst expecting a login failure. 
  //      driver.findElement(By.name("submit")).submit();

        // Return a new page object representing the destination. Should the login page ever
        // go somewhere else (for example, a legal disclaimer) then changing the method signature
        // for this method will mean that all tests that rely on this behaviour won't compile.
        return new HomePage(driver);    
    }

    // The login page allows the user to submit the login form knowing that an invalid username and / or password were entered
    public LoginPage submitLoginExpectingFailure() {
        // This is the only place that submits the login form and expects the destination to be the login page due to login failure.
    //    driver.findElement(By.name("submit")).submit();

        // Return a new page object representing the destination. Should the user ever be navigated to the home page after submiting a login with credentials 
        // expected to fail login, the script will fail when it attempts to instantiate the LoginPage PageObject.
        return new LoginPage(driver);   
    }

    // Conceptually, the login page offers the user the service of being able to "log into"
    // the application using a user name and password. 
    public HomePage loginAs(String username, String password) {
        // The PageObject methods that enter username, password & submit login have already defined and should not be repeated here.
        typeUsername(username);
        typePassword(password);
        return submitLogin();
    }
    public static void main(String[] args) {
        WebDriver driver = new FirefoxDriver();

        // And now use this to visit Google
        driver.get("URL Goes Here");
        LoginPage login = new LoginPage(driver);
        HomePage a=login.loginAs("username","Password");


    }
} 

I reffered it from http://code.google.com/p/selenium/wiki/PageObjects

6
  • Have you provided any URL at line driver.get("URL Goes Here");? Commented Nov 26, 2013 at 20:39
  • Yes I provided the URL there just removed it while posting here. Commented Nov 26, 2013 at 20:40
  • the page was getting loaded but nothing was happening after that. Commented Nov 26, 2013 at 20:41
  • 2
    @Jay From your stacktrace you have provided. it saying that you code is unable to locate those elements. May be username and password may not be having those attributes. Try to get XPATH or ID instead of name attributes. Or atleast make sure those attributes are present. Commented Nov 26, 2013 at 20:53
  • 1
    I just checked name is present <input type="text" validation_error="" validation="_required" class="_username" name="username">, <input type="password" name="password"> Commented Nov 26, 2013 at 21:04

1 Answer 1

1

Try to add

try {
    Thread.sleep(5000);
} catch (InterruptedException e) {
    e.printStackTrace();
}

before HomePage a=login.loginAs("username","Password");.

It is not quite good approach, but it will help to find out where is the problem. This is just a pause before next step. You shouldn't use it in future, because it is not pretty good practice. It is better to use wait for condition check this out for more information.

Sign up to request clarification or add additional context in comments.

4 Comments

Yes I was able to see the username/password in the text box. but the submit button isnt getting clicked.Thanks!
There is no code for clicking submit button. You should add it. Something like driver.findElement(By.name("submit")).click();
@Jay Dont use Thread.sleep(5000), instead use Explicit and Implicit Waits
You should replace this with wait for condition, because this pauses are not good practice. Check this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.