如何使用selenium登录gmail?我的代码无效[重复]

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用selenium登录gmail?我的代码无效[重复]相关的知识,希望对你有一定的参考价值。

这个问题在这里已有答案:

我无法使用我的代码登录Gmail。请帮忙

public class first_test {
    WebDriver driver = new ChromeDriver();

    @BeforeTest
    public void tearup()
    {
        WebDriver driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
        driver.get("https://accounts.google.com");

    }

  @Test
  public void mail() throws InterruptedException  {
      WebElement email=driver.findElement(By.id("identifierId"));
      email.click();
      email.sendKeys("sharfulumair");
      WebElement password=driver.findElement(By.id("//*[@name="password"]"));
      password.click();
      password.sendKeys("abcd");
  }


  @AfterTest  
  public void teardown()
  {
    driver.close();
  }


}

我得到的错误信息是:

无法找到元素:{“method”:“id”,“selector”:“identifierId”}

答案

在访问webelement之前尝试使用wait。

public void mail() throws InterruptedException  {
      new WebDriverWait(driver, 60).until(ExpectedConditions.presenceOfElementLocated(By.id("identifierId")));
      WebElement email=driver.findElement(By.id("identifierId"));
      ...............
  }

以上是关于如何使用selenium登录gmail?我的代码无效[重复]的主要内容,如果未能解决你的问题,请参考以下文章

如何使用Selenium java通过GMail自动发送附件

尝试通过 Selenium 和 Python 使用 GeckoDriver Firefox 登录 Gmail 帐户时出现“此浏览器或应用程序可能不安全”错误

如何在Selenium中单击带有锚标记的图像或图标

使用 smptlib 登录我的 gmail 帐户时出现身份验证错误

用户从 Android 中的 Gmail 登录后如何获取访问令牌?

保存需要登录的网页源代码