这个问题在这里已有答案:
我无法使用我的代码登录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"));
...............
}