无法在Selenium中找到Element By ID

mee*_*pin 2 java selenium selenium-webdriver

我无法使用selenium查找登录ID.我之前在Windows计算机上完成了这项工作,但是我想在家里在我的Mac上做这个,我不能再找到id的元素了.我已经尝试实现了很多人在线建议的driverwait,但我仍然遇到同样的错误.任何帮助将不胜感激.

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();
    }

}
Run Code Online (Sandbox Code Playgroud)

登录页面对象如下:

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");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助.

错误信息

org.openqa.selenium.NoSuchElementException:无法定位元素:{"method":"id","selector":"login-form-username"}命令持续时间或超时:13毫秒

Abh*_*ngh 8

原因1:

等待加载元素.使用

WebDriverWait wait = new WebDriverWait(driver, 4000);
wait.until(ExpectedConditions.visibilityOfElementLocated((By.id("id"))));
Run Code Online (Sandbox Code Playgroud)

原因2:

检查是否<input id="id" class="p-field span12" type="text">在任何框架中.

如果是,请使用

driver.switchTo.frame("frameName"); 
Run Code Online (Sandbox Code Playgroud)

在使用之前

driver.findElement(By.id("id")).sendKeys("input key");
Run Code Online (Sandbox Code Playgroud)