Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to locate an Element By ID in Selenium

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

like image 888
meepin Avatar asked Dec 26 '22 17:12

meepin


1 Answers

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");
like image 165
Abhishek Singh Avatar answered Dec 28 '22 06:12

Abhishek Singh