Gmail - 网络驱动程序自动化

时间:2017-01-10 04:13:30

标签: xpath selenium-webdriver

以下是我尝试通过网络驱动程序自动化Gmail的代码。

我发现了一些奇怪的东西。每当我评论该行以找到Password (Driver.findElement(By.xpath(".//*[@id='Passwd']")).sendKeys("SRS");)

然后网络驱动程序成功点击“Sign In”按钮

但是当我取消对该行的评论时

(Driver.findElement(By.xpath(".//*[@id='Passwd']")).sendKeys("SRS");) 

然后Web驱动程序也无法单击“登录”按钮,它会显示错误消息Unable to find the Password Xpath但仍然只在电子邮件ID屏幕上

Here is the attached截图

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);


    }

}

2 个答案:

答案 0 :(得分:0)

1。您提供了错误的电子邮件ID,因此Google对其进行了验证,并将错误消息显示为“抱歉,Google无法识别该电子邮件”。

因此,请输入有效的电子邮件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);
   }
}

答案 1 :(得分:0)

请添加"登录"按钮也,我测试了这个。这工作正常。    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();