package mypac;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.*;
import org.openqa.selenium.WebDriver;
public class Mytest {
public static void main(String[] args) {
String baseurl = "http://www.timecrunch.me";
WebDriver driver = new FirefoxDriver();
System.setProperty("webdriver.chrome.driver", "D:\\chromedriver_win32\\chromedriver.exe");
WebDriver driver1 = new ChromeDriver();
driver.get(baseurl);
driver.findElement(By.name("wpusername")).sendKeys("ankitramola37");
driver.findElement(By.name("wppassword")).
sendKeys("ongrapha");
driver.findElement(By.id("logoutBtn")).click();
driver.findElement(By.id("fcNewEventTitle ")).click();
driver.findElement(By.id("fcNewEventTitle ")).sendKeys("birthday ");
driver1.get(baseurl);
driver1.findElement(By.name("wpusername ")).sendKeys("ankitramola37 ");
driver1.findElement(By.name("wppassword ")).sendKeys("ongrapha ");
driver1.findElement(By.id("logoutBtn ")).click();
driver1.findElement(By.id("fcNewEventTitle ")).click();
driver1.findElement(By.id("fcNewEventTitle ")).sendKeys("birthday ");
}
}
尝试了此代码但无法运行
exception Starting ChromeDriver (v2.7.236900) on port 19245
Exception in thread "main " org.openqa.selenium.NoSuchElementException: Unable to locate
element: {"
method ":"
id ","
selector ":"
fcNewEventTitle "}
Command duration or timeout: 12.47 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions
/no_such_element.html
Build info: version: '2.39.0', revision: 'ff23eac', time: '2013-12-16 16:12:12'
System info: host: 'Comp-PC', ip: '192.168.2.28', os.name: 'Windows 7', os.arch: 'x86',
os.version: '6.1', java.version: '1.7.0_51'
Session ID: 5ff1488c-950a-47e2-8f80-a62bcc5766d7
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=XP, acceptSslCerts=true, javascriptEnabled=true,
cssSelectorsEnabled=true, databaseEnabled=true, browserName=firefox, handlesAlerts=true
, browserConnectionEnabled=true, webStorageEnabled=true, nativeEvents=true,
rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true
, takesScreenshot=true, version=26.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:193)
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:220)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:299)
at mypac.Mytest.main(Mytest.java:25)
Caused by: org.openqa.selenium.remote.ErrorHandler$UnknownServerException: Unable to
locate element: {"
method ":"
id ","
selector ":"
fcNewEventTitle "}
Build info: version: '2.39.0', revision: 'ff23eac', time: '2013-12-16 16:12:12'
System info: host: 'Comp-PC', ip: '192.168.2.28', os.name: 'Windows 7', os.arch: 'x86',
os.version: '6.1', java.version: '1.7.0_51'
Driver info: driver.version: unknown
at <anonymous class>.FirefoxDriver.prototype.findElementInternal_(file:///C:/User
s/Comp/AppData/Local/Temp/anonymous1178081315003174620webdriver-profile/extensions
/fxdriver@googlecode.com/components/driver_component.js:8860)
at <anonymous class>.FirefoxDriver.prototype.findElement(file:///C:/Users
/Comp/AppData/Local/Temp/anonymous1178081315003174620webdriver-profile/extensions
/fxdriver@googlecode.com/components/driver_component.js:8869)
at <anonymous class>.DelayedCommand.prototype.executeInternal_/h(file:///C:/Users
/Comp/AppData/Local/Temp/anonymous1178081315003174620webdriver-profile/extensions
/fxdriver@googlecode.com/components/command_processor.js:10831)
at <anonymous class>.DelayedCommand.prototype.executeInternal_(file:///C:/Users
/Comp/AppData/Local/Temp/anonymous1178081315003174620webdriver-profile/extensions
/fxdriver@googlecode.com/components/command_processor.js:10836)
at <anonymous class>.DelayedCommand.prototype.execute/<(file:///C:/Users
/Comp/AppData/Local/Temp/anonymous1178081315003174620webdriver-profile/
/fxdriver@googlecode.com/components/command_processor.js:107`enter code here`78)
答案 0 :(得分:2)
该错误是由于您尝试查找fcNewEventTitle
的元素不存在而导致此错误:
Unable to
locate element: {"method":"id","selector":"fcNewEventTitle"}
. . . .
在你做这样的事情之前:
driver1.findElement(By.id("fcNewEventTitle")).click();
等待那个元素。有关详细信息,请参阅here。