我的代码是
public static void main(String[] args) {
// declaration and instantiation of objects/variables
WebDriver driver = new FirefoxDriver();
driver.navigate().to("http://gmail.com");
System.out.println(driver.getTitle());
WebElement element = driver.findElement(By.xpath("//*[@id='Email']"));
element.sendKeys("asangal90@gmail.com");
element = driver.findElement(By.name("signIn"));
element.click();
element = driver.findElement(By.xpath("//*[@id='Passwd']"));
element.sendKeys("123456");
element = driver.findElement(By.name("signIn"));
element.click();
}
错误是:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//*[@id='Passwd']"}
Command duration or timeout: 64 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.52.0', revision: '4c2593c', time: '2016-02-11 19:03:33'
System info: host: 'CoolAkku-PC', ip: '10.209.176.3', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.7.0_80'
*** Element info: {Using=xpath, value=//*[@id='Passwd']}
Session ID: 72a7bd1d-e9e9-4ac7-8c1c-c91c1717990d
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=WINDOWS, acceptSslCerts=true, javascriptEnabled=true, cssSelectorsEnabled=true, databaseEnabled=true, browserName=firefox, handlesAlerts=true, nativeEvents=false, webStorageEnabled=true, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=37.0}]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:363)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:500)
at org.openqa.selenium.By$ByXPath.findElement(By.java:361)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:355)
at myclass.main(myclass.java:22)
Caused by: org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//*[@id='Passwd']"}
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.52.0', revision: '4c2593c', time: '2016-02-11 19:03:33'
System info: host: 'CoolAkku-PC', ip: '10.209.176.3', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.7.0_80'
Driver info: driver.version: unknown
at <anonymous class>.FirefoxDriver.prototype.findElementInternal_(file:///C:/Users/COOLAK~1/AppData/Local/Temp/anonymous207772722037832049webdriver-profile/extensions/fxdriver@googlecode.com/components/driver-component.js:10723)
at <anonymous class>.FirefoxDriver.prototype.findElement(file:///C:/Users/COOLAK~1/AppData/Local/Temp/anonymous207772722037832049webdriver-profile/extensions/fxdriver@googlecode.com/components/driver-component.js:10732)
at <anonymous class>.DelayedCommand.prototype.executeInternal_/h(file:///C:/Users/COOLAK~1/AppData/Local/Temp/anonymous207772722037832049webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12614)
at <anonymous class>.DelayedCommand.prototype.executeInternal_(file:///C:/Users/COOLAK~1/AppData/Local/Temp/anonymous207772722037832049webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12619)
at <anonymous class>.DelayedCommand.prototype.execute/<(file:///C:/Users/COOLAK~1/AppData/Local/Temp/anonymous207772722037832049webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12561)
答案 0 :(得分:1)
目前,驱动程序不会等待DOM中存在的元素。定义隐式等待以告诉驱动程序在抛出异常之前等待一段时间
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
这将告诉司机搜索最多5秒的元素。