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

问题描述 投票:0回答:1

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

我无法使用我的代码登录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”}

selenium selenium-webdriver
1个回答
0
投票

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

public void mail() throws InterruptedException  {
      new WebDriverWait(driver, 60).until(ExpectedConditions.presenceOfElementLocated(By.id("identifierId")));
      WebElement email=driver.findElement(By.id("identifierId"));
      ...............
  }
© www.soinside.com 2019 - 2024. All rights reserved.