如何解决org.openqa.selenium.NoSuchElementException:没有这样的元素

时间:2014-04-25 07:05:24

标签: eclipse excel selenium-webdriver testng selenium-chromedriver

我正在尝试使用excel表登录facebook。在这个程序中,excelsheet导入用户名和密码,但是当登录功能调用时它会显示错误,它将显示没有这样的元素错误 我的Excel工作表

enter image description here

    FileInputStream fi=new FileInputStream("E:\\workspace99\\SeleniumAutomations\\OR\\Login_or.xls");
          Workbook w=Workbook.getWorkbook(fi);
          Sheet s=w.getSheet("login");
          driver.findElement(By.id(s.getCell(0,1).getContents())).sendKeys("rishy9999@gmail.com");
          driver.findElement(By.id(s.getCell(1,1).getContents())).sendKeys("chandu");
          Thread.sleep(1000);
          driver.findElement(By.id(s.getCell(2,1).getContents())).click();
          Thread.sleep(5000);
it throws:

    org.openqa.selenium.NoSuchElementException: no such element
      (Session info: chrome=34.0.1847.116)
      (Driver info: chromedriver=2.6.232923,platform=Windows NT 6.1 x86) (WARNING: The server did not provide any stacktrace information)
    Command duration or timeout: 61 milliseconds
    For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
    Build info: version: '2.33.0', revision: '4e90c97', time: '2013-05-22 15:32:38'
    System info: os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.7.0_01'
    Session ID: 8258b613a2b39bccb3f9fa30624d3acd
    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_dir3768_17298}, rotatable=false, locationContextEnabled=true, version=34.0.1847.116, 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.Login2.f(Login2.java:25)
        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 :(得分:0)

请检查您的Facebook所选语言是'英语(美国)'以及您尝试运行的浏览器默认语言环境是否在en_US上。我已经看到了不同语言环境中“id”的变化。例如,我已经使用法语检查了浏览器区域设置并启动了facebook。 “登录”按钮的ID为“u_0_l”。对于英语(美国),它是'u_0_n'。我希望这可能是这种例外的原因之一。