无法在Selenium中按ID定位元素

无法在Selenium中按ID定位元素

问题描述:

我无法使用硒查找登录ID.我之前是在Windows计算机上完成这项工作的,但是我试图在Mac上在家完成这项工作,而现在我无法再通过id找到该元素.我已经尝试实现driverwaitwait,这在网上很多人都建议过,但是我仍然遇到相同的错误.任何帮助将不胜感激.

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

感谢您的帮助.

错误消息

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

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

原因1:

等待要加载的元素.使用

Waiting for the element to be loaded. Use

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

原因2:

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

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

如果是,请使用

driver.switchTo.frame("frameName"); 

使用

driver.findElement(By.id("id")).sendKeys("input key");