2014-04-04 69 views
1

我試圖導入登錄憑證,如:usernamepassword使用XSL表,使用下面的代碼,但我得到了NoSuchElementException異常運行時,org.openqa.selenium.NoSuchElementException

我的XSL表如下:

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

現在用:

  • 日食
  • webdriver.iostrea米
  • 硒工具

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

如果您聽到這個問題,因爲當你get("http://gmail.com/") ID用("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"); 
    } 

此外,你點擊登錄按鈕時要注意,你正在尋找與文本"input.signin"一個鏈接,我認爲你的意思做的,是一個CSS選擇器input帶班的signin但這ele也不存在。

+0

非常感謝你 – rishy

相關問題