简体   繁体   English

Selenium WebDriver无法捕获Gmail登录的错误消息

[英]Selenium WebDriver fails to capture error message of gmail login

I am trying to capture the error message on gmail login If I do not populate the user name and click on Next button, then "Please enter your email" message appears, but Selenium gives the error message- argument is not found. 我试图捕获gmail登录时的错误消息如果未填充用户名并单击“下一步”按钮,则会出现“请输入您的电子邮件”消息,但Selenium给出了错误消息-找不到自变量。 My code is: 我的代码是:

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;

public class Msg 
{
    public static void main(String[] args) throws InterruptedException
    {
    WebDriver driver=new FirefoxDriver();   
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    driver.manage().window().maximize();
    driver.get("http://www.gmail.com/");
    driver.findElement(By.xpath(".//*[@id='next']")).click();
              Thread.sleep(2000);   
    WebElement ele=driver.findElement(By.xpath(".//*[@id='next']"));
    String js="argument[0].style.height='auto';     argument[0].style.visibility='visible';";
    ((JavascriptExecutor)driver).executeScript(js,ele);

    String actual=ele.getText();
    System.out.println(actual);
    //String expected= "Please enter your email";
    Assert.assertEquals(actual, "Please enter your email");
    System.out.println("Pass");
    }
}

Try this code 试试这个代码

import java.util.concurrent.TimeUnit;
    import org.openqa.selenium.By;
    import org.openqa.selenium.JavascriptExecutor;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.testng.Assert;

    public class Msg 
    {
    public static void main(String[] args) throws InterruptedException
    {
        System.setProperty("webdriver.chrome.driver", "D:\\Selenium\\LMS_Selenium\\chromedriver.exe");
    WebDriver driver=new ChromeDriver();   
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    driver.manage().window().maximize();
    driver.get("http://www.gmail.com/");
    driver.findElement(By.xpath(".//*[@id='next']")).click();
              Thread.sleep(2000);   
    WebElement ele=driver.findElement(By.xpath(".//*[@id='next']"));
    WebElement error=driver.findElement(By.xpath(".//*[@class='error-msg']"));
    String actual=error.getText();
    System.out.println(actual);
    //String expected= "Please enter your email";
    Assert.assertEquals(actual, "Please enter your email.");
    System.out.println("Pass");
    }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM