java 使用 Selenium webdriver 登录 Gmail 失败。显示未找到密码的元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33318653/
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-11-02 21:30:48  来源:igfitidea点击:

Gmail login fail using Selenium webdriver. Showing element not found for password

javaseleniumselenium-webdriver

提问by Deepak

public static void main(String[] args){
    System.setProperty("webdriver.chrome.driver","E:/softwares/chromedriver_win32/chromedriver.exe");
    WebDriver gmail= new ChromeDriver();

    gmail.get("https://www.gmail.co.in"); 
    gmail.findElement(By.id("Email")).sendKeys("abcd");
    gmail.findElement(By.id("next")).click();
    gmail.findElement(By.id("Passwd")).sendKeys("xyz");

采纳答案by JRodDynamite

Try setting an implicit wait of maybe 10 seconds.

尝试设置可能 10 秒的隐式等待。

gmail.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

Or set an explicit wait. An explicit waits is code you define to wait for a certain condition to occur before proceeding further in the code. In your case, it is the visibility of the password input field. (Thanks to ainlolcat's comment)

或者设置显式等待。显式等待是您定义的代码,用于在进一步处理代码之前等待特定条件发生。在您的情况下,它是密码输入字段的可见性。(感谢ainlolcat评论

WebDriverWait wait = new WebDriverWait(gmail, 10);
WebElement element = wait.until(
    ExpectedConditions.visibilityOfElementLocated(By.id("Passwd")));
gmail.findElement(By.id("Passwd")).sendKeys("xyz");

Explanation:The reason selenium can't find the element is because the idof the password input field is initially Passwd-hidden. After you click on the "Next" button, Google first verifies the email address entered and then shows the password input field (by changing the id from Passwd-hiddento Passwd). So, when the password field is still hidden (i.e. Google is still verifying the email id), your webdriver starts searching for the password input field with id Passwdwhich is still hidden. And hence, an exception is thrown.

说明:selenium 找不到元素的原因是因为id密码输入字段的 最初是Passwd-hidden。单击“下一步”按钮后,Google 首先验证输入的电子邮件地址,然后显示密码输入字段(通过将 id 更改Passwd-hiddenPasswd)。因此,当密码字段仍处于隐藏状态时(即 Google 仍在验证电子邮件 ID),您的网络驱动程序将开始搜索 idPasswd仍处于隐藏状态的密码输入字段。因此,抛出异常。

回答by mounika

Thread.sleep(); worked for me .

线程.sleep(); 对我来说有效。

WebDriver driver=new FirefoxDriver();
        driver.get("https:www.google.com");
        driver.findElement(By.linkText("Gmail")).click();
        driver.findElement(By.linkText("SIGN IN")).click();
        driver.findElement(By.id("Email")).sendKeys("abcde");
        driver.findElement(By.id("next")).click();
        Thread.sleep(2000);
    WebElement password = driver.findElement(By.xpath(".//*[@id='Passwd']"));
    password.sendKeys("xyzzz");
        driver.findElement(By.id("signIn")).click();
        Thread.sleep(4000);
       driver.close();

回答by Zahid Iqbal

driver.get("http://mail.google.com");
driver.findElement(By.xpath("//input[@id='identifierId']")).sendKeys("emaild");
driver.findElement(By.xpath("//span[contains(text(),'Next')]")).click();
Thread.sleep(2000);
WebElement passwordEnter = driver.findElement(By.xpath("//input[@name='password']"));
passwordEnter.sendKeys(password);
driver.findElement(By.xpath("//span[contains(text(),'Next')]")).click();