这是我试图通过Web驱动自动化Gmail的代码。
我发现了一些奇怪的东西。每当我注释掉这行以查找Password (Driver.findElement(By.xpath(".//*[@id='Passwd']")).sendKeys("SRS");)时
则Web Driver成功地点击了"Sign In“按钮
但当我取消注释这行代码时
(Driver.findElement(By.xpath(".//*[@id='Passwd']")).sendKeys("SRS");) 则Web Driver也不能单击登录按钮,并且它会给出错误消息,即Unable to find the Password Xpath仍然只显示在电子邮件id屏幕上。
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Gmail {
public static void main(String[] args) {
// TODO Auto-generated method stub
WebDriver Driver = new FirefoxDriver();
Driver.get("https://www.google.com/gmail/about/");
Driver.findElement(By.xpath("html/body/nav/div/a[2]")).click();
Driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
//Enter the Gmail ID
Driver.findElement(By.xpath(".//*[@id='Email']")).sendKeys("RK12@gmail.com");
Driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
//Click on Next Button
Driver.findElement(By.xpath(".//*[@id='next']")).click();
Driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
Driver.findElement(By.xpath(".//*[@id='Passwd']")).sendKeys("SRS");
//Driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
}
}发布于 2017-01-10 15:55:43
1.你给出了错误的电子邮件id,因此谷歌对其进行了验证,并显示错误消息为“对不起,谷歌无法识别该电子邮件”。
因此,请输入有效的电子邮件id才能进入密码页面。
2.这里的与问题中的代码一起提供了ElementNotVisibleException,因此添加了ExpectedConditions.visibilityOfElementLocated,以确保在发送密钥之前加载了密码字段。
更新代码:
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Gmail {
public static void main(String[] args) {
WebDriver Driver = new FirefoxDriver();
Driver.get("https://www.google.com/gmail/about/");
Driver.findElement(By.xpath("html/body/nav/div/a[2]")).click();
Driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
//Enter the Gmail ID
Driver.findElement(By.xpath(".//*[@id='Email']")).sendKeys("rbnaveen558@gmail.com");
Driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
//Click on Next Button
Driver.findElement(By.xpath(".//*[@id='next']")).click();
Driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
WebDriverWait wait = new WebDriverWait(Driver, 10);
WebElement pwd = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("Passwd")));
pwd.sendKeys("SRS");
//Driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
}
}发布于 2017-01-10 16:25:20
请添加“登录”按钮,我测试的this.This是正常的。driver.findElement(By.xpath(".//*[@id='Passwd']")).sendKeys("SRS"); driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS); //click to sign in driver.findElement(By.id("signIn")).click();
https://stackoverflow.com/questions/41561025
复制相似问题