问题描述
部分代码:WebDriverdriver=newInternetExplorerDriver();driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);WebElementusername=driver.findElement(By.name("j_username"));username.sendKeys(name);WebElementpassword=driver.findElement(By.name("j_password"));password.sendKeys(passwd);driver.findElement(By.name("Submit3")).click();出现下面异常:Exceptioninthread"main"org.openqa.selenium.NoSuchElementException:Unabletofindelementwithname==j_username(WARNING:Theserverdidnotprovideanystacktraceinformation)Commanddurationortimeout:30.24secondsFordocumentationonthiserror,pleasevisit:http://seleniumhq.org/exceptions/no_such_element.htmlBuildinfo:version:'2.25.0',revision:'17482',time:'2012-07-1821:09:54'Systeminfo:os.name:'WindowsXP',os.arch:'x86',os.version:'5.1',java.version:'1.6.0_05'Driverinfo:driver.version:RemoteWebDriverSessionID:cb2d2d07-f4d3-458a-9702-305aedf03b94atsun.reflect.NativeConstructorAccessorImpl.newInstance0(NativeMethod)atsun.reflect.NativeConstructorAccessorImpl.newInstance(UnknownSource)atsun.reflect.DelegatingConstructorAccessorImpl.newInstance(UnknownSource)atjava.lang.reflect.Constructor.newInstance(UnknownSource)atorg.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:188)atorg.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)atorg.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:498)atorg.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:268)atorg.openqa.selenium.remote.RemoteWebDriver.findElementByName(RemoteWebDriver.java:329)atorg.openqa.selenium.By$ByName.findElement(By.java:291)atorg.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:260)atTry.Login.Login(Login.java:109)atTry.Login.main(Login.java:51)但是将代码中的第一行换成:WebDriverdriver=newFirefoxDriver();则能够正常运行
解决方案
解决方案二:
楼主我也遇到这个问题了
解决方案三:
你页面的j_username这个元素肯定是这样写的<inputid="j_username"type="text"value="vvv">
driver.findElement(By.name("j_username"));其实就是调用以下的javascriptdocument.getElementsByName("j_username");
IE和firefox的javascript的引擎不一样,ie的场合找不到name的话会去找idfirefox就直接返回null了。所以把页面的id改成name就行了
解决方案四:
或者webdriver的代码改成WebElementusername=driver.findElement(By.id("j_username"));