0

我正在尝试使用功能登录 gmail。我正在从 Excel 表中传递用户名。问题是我的代码返回值但没有在文本框中输入。它不会抛出任何错误,只是返回空异常。

请帮帮我,我是 selenium webdriver 的新手,真的不知道下一步该做什么,因为它也没有显示任何错误。

public class gmail 
 {

public static WebDriver driver;

       public static void main(String[] args) throws IOException, InterruptedException 
{
    File file1 = new      File("C:\\Selenium\\IEDriverServer_Win32_2.35.3\\IEDriverServer.exe"); 
  System.setProperty("webdriver.ie.driver", file1.getAbsolutePath());
  WebDriver driver = new InternetExplorerDriver();
  driver.get("www.gmail.com");    



      FileInputStream file = new FileInputStream(new File("D:\\Automation\\Selenium\\New Folder\\Demo\\Book2.xls"));   // Path of the excel where the keywords and data was mentioned
      HSSFWorkbook workbook = new HSSFWorkbook(file);    
      HSSFSheet sheet = workbook.getSheetAt(0);
      int d= sheet.getLastRowNum();
      System.out.println(d);   


     for (int i=1;i<=d;i++)
      {
           Cell cell1=null;
            cell1=sheet.getRow(i).getCell(0);
            System.out.println(cell1);

            if (cell1.getStringCellValue().contains("text"))      
            {
                  Cell cell2=null;
                  cell2=sheet.getRow(i).getCell(1);
                  System.out.println(cell2);
                  stg(cell2);   //calling function                 

            }    

      }     

}  

public static void stg(Cell cell2) throws InterruptedException 
{
  WebElement un1=driver.findElement(By.name("Email"));
  System.out.println(un1);
  un1.sendKeys(cell2.getStringCellValue());

 }

}

 //This is the Output which i am getting:
 1
text

选择测试10j

  Exception in thread "main" java.lang.NullPointerException
at Excel.gmail.stg(gmail.java:63)
at Excel.gmail.main(gmail.java:53)
4

2 回答 2

0

我强烈建议不要使用 Selenium 自动化 Gmail。它非常复杂,您应该专注于自动化您的 OWN 应用程序而不是 gmail。使用 gmail POP3 服务器检索电子邮件等。这是我对类似问题的回答。

于 2013-10-05T17:24:41.957 回答
0

似乎您没有初始化类字段driver

public static WebDriver driver;

同时,在方法内部声明一个同名main的局部变量:

WebDriver driver = new InternetExplorerDriver();

当代码工作时,方法中的代码是否在stg里面main?这会起作用,因为局部变量实际上已被初始化。当您将代码移到 main 之外时,局部变量不再隐藏类字段,并且由于您没有对其进行初始化,您会得到 NullPointerException。

请参阅以了解有关字段和局部变量的更多信息,以及了解有关隐藏字段的信息。

于 2013-10-04T12:48:17.170 回答