Java 无法在 Selenium 中通过 ID 定位元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20083357/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 22:59:11  来源:igfitidea点击:

Unable to locate an Element By ID in Selenium

javaseleniumselenium-webdriver

提问by meepin

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.

我在使用 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();
    }

}

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

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

采纳答案by Abhishek Singh

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:

原因2:

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

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

If yes use

如果是,请使用

driver.switchTo.frame("frameName"); 

before using

使用前

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