5

I am having trouble locating the login id using selenium. I got this work on a windows computer before, but I am trying to do this at home on my mac and I am not able to find the element by id anymore. I have tried implementing driverwait what was suggested by a lot of people online, but I am still encountering the same errors. Any help will be appreciated.

public class mainEntry {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String webPage;

        //theDriver driverCMD = new theDriver();
        WebDriver driverCMD = new FirefoxDriver();

        login start = new login("https://jira.arbonne.com/", driverCMD);
        start.loginWithUser();
    }

}

The Login page object is below:

public class login {
    String webpage;
    WebDriver driverCMD;

    login(String webpage, WebDriver driverCMD)
    {
        this.webpage = webpage;
        this.driverCMD = driverCMD;
    }

    public void loginWithUser()
    {
        WebDriverWait wait = new WebDriverWait(driverCMD, 300); // The int here is the maximum time in seconds the element can wait.
        try
        {
            driverCMD.get(webpage);
            //driverCMD.driver.get(webpage);

        }
        catch(Exception e)
        {
            System.out.print("could not get webpage");
        }

        try{
            wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("login-form-username")));

            WebElement username = driverCMD.findElement(By.id("login-form-username"));
            username.sendKeys("test");
            //WebElement password = driverCMD.driver.findElement(By.id("login-form-password"));
            //password.sendKeys("test");
            //password.submit();
        }
        catch(Exception e)
        {
            System.out.println("could not login");
        }
    }
}

Thank you for you help.

Error message

org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"id","selector":"login-form-username"} Command duration or timeout: 13 milliseconds

Arran
  • 24,648
  • 6
  • 68
  • 78
meepin
  • 65
  • 1
  • 1
  • 7
  • When you catch the exception, what is the message and stacktrace of the exception that is thrown? That can provide more details on what might be causing the error. – Nathan Dace Nov 19 '13 at 22:57
  • org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"id","selector":"login-form-username"} Command duration or timeout: 13 milliseconds – meepin Nov 19 '13 at 23:40
  • 1
    Are you sure that this element is on the page?Do you see your Firefox waiting for that element appear during testing? – Petr Mensik Nov 20 '13 at 08:43

1 Answers1

11

Reason 1:

Waiting for the element to be loaded. Use

WebDriverWait wait = new WebDriverWait(driver, 4000);
wait.until(ExpectedConditions.visibilityOfElementLocated((By.id("id"))));

Reason 2:

Check to see if <input id="id" class="p-field span12" type="text"> is in any frame.

If yes use

driver.switchTo.frame("frameName"); 

before using

driver.findElement(By.id("id")).sendKeys("input key");
Abhishek Singh
  • 10,243
  • 22
  • 74
  • 108