org.openqa.selenium.NoSuchElementException

时间:2014-04-04 10:36:46

标签: java eclipse selenium xslt nosuchelementexception

我正在尝试使用xsl表导入登录凭据,如:usernamepassword,使用下面的代码,但运行时出现NoSuchElementException异常,

我的xsl表格如下:

  username    |  password
--------------------------
jan30selenium | selenium

我正在使用:

  • webdriver.iostream
  • selenium tool

Login.java:

package Iostream;

import java.io.FileInputStream;

import jxl.Sheet;
import jxl.Workbook;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;

public class Login {

  public WebDriver driver;

  @Test
  public void f()throws Exception {
      FileInputStream fi=new FileInputStream("E:\\workspace1\\SeleniumAutomation\\test data\\login.xls");
      Workbook w= Workbook.getWorkbook(fi);
      Sheet s=w.getSheet(0);
      driver.findElement(By.id("f_id")).sendKeys(s.getCell(0,1).getContents());
      driver.findElement(By.id("f_pwd")).sendKeys(s.getCell(1,0).getContents());
      Thread.sleep(5000);
      driver.findElement(By.linkText("input.signin")).click();
      Thread.sleep(3000);
  }

  @BeforeTest
  public void beforeTest() {
      System.setProperty("webdriver.chrome.driver","\\E:\\lib\\chromedriver.exe");
      driver=new ChromeDriver();
      driver.get("http://www.gmail.com");
  }

  @AfterTest
  public void afterTest() {
  }
}

例外是:

Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{platform=XP, acceptSslCerts=true, javascriptEnabled=true, browserName=chrome, chrome={userDataDir=C:\Users\User\AppData\Local\Temp\scoped_dir5352_17443}, rotatable=false, locationContextEnabled=true, version=33.0.1750.154, takesHeapSnapshot=true, cssSelectorsEnabled=true, databaseEnabled=false, handlesAlerts=true, browserConnectionEnabled=false, webStorageEnabled=true, nativeEvents=true, applicationCacheEnabled=false, takesScreenshot=true}]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:191)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:554)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:307)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementById(RemoteWebDriver.java:348)
    at org.openqa.selenium.By$ById.findElement(By.java:216)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:299)
    at Iostream.Login.f(Login.java:23)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
    at org.testng.TestRunner.privateRun(TestRunner.java:767)
    at org.testng.TestRunner.run(TestRunner.java:617)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
    at org.testng.SuiteRunner.run(SuiteRunner.java:240)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
    at org.testng.TestNG.run(TestNG.java:1057)
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)

问题出在哪里?

1 个答案:

答案 0 :(得分:1)

您遇到了这个问题,因为当您get("http://gmail.com/")时,"f_id""f_pwd"不在那里。

如果您打算登录,那么您应该更具体地使用您的网址,并且您应该使用以下代码:

  @Test
  public void f()throws Exception {
      FileInputStream fi=new FileInputStream("E:\\workspace1\\SeleniumAutomation\\test data\\login.xls");
      Workbook w= Workbook.getWorkbook(fi);
      Sheet s=w.getSheet(0);
      driver.findElement(By.id("Email")).sendKeys(s.getCell(0,1).getContents());
      driver.findElement(By.id("Passwd")).sendKeys(s.getCell(1,0).getContents());
      Thread.sleep(5000);
      driver.findElement(By.cssSelector("input#signIn")).click();
      Thread.sleep(3000);
      }
  @BeforeTest
  public void beforeTest() {
      System.setProperty("webdriver.chrome.driver","\\E:\\lib\\chromedriver.exe");
      driver=new ChromeDriver();
      driver.get("https://accounts.google.com/ServiceLogin?service=mail");
  }

另外,您在点击登录按钮时会注意到,您正在寻找与TEXT "input.signin"的链接我认为您的意图是CSS选择器inputsignin,但该元素也不存在。