2011-09-16 83 views
4

我想選擇在this website的性別字段。我用下面的代碼:無法與ID找到元素即使壽它的存在

driver.get("https://edit.yahoo.com/registration"); 
    WebElement dropDownListBox = driver.findElement(By.id("gender")); 

我得到以下異常:

org.junit.experimental.theories.internal.ParameterizedAssertionError: testGender(HTML_DRIVER) 
at org.junit.experimental.theories.Theories$TheoryAnchor.reportParameterizedError(Theories.java:183) 
at org.junit.experimental.theories.Theories$TheoryAnchor$1$1.evaluate(Theories.java:138) 
at org.junit.experimental.theories.Theories$TheoryAnchor.runWithCompleteAssignment(Theories.java:119) 
at org.junit.experimental.theories.Theories$TheoryAnchor.runWithAssignment(Theories.java:103) 
at org.junit.experimental.theories.Theories$TheoryAnchor.runWithIncompleteAssignment(Theories.java:112) 
at org.junit.experimental.theories.Theories$TheoryAnchor.runWithAssignment(Theories.java:101) 
at org.junit.experimental.theories.Theories$TheoryAnchor.evaluate(Theories.java:89) 
at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79) 
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71) 
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49) 
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) 
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) 
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) 
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) 
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) 
at org.junit.runners.ParentRunner.run(ParentRunner.java:236) 
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) 
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) 
Caused by: org.openqa.selenium.NoSuchElementException: Unable to locate element with ID: gender 
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html 
Build info: version: '2.6.0', revision: '13840', time: '2011-09-13 14:56:25' 
System info: os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.6.0_26' 
Driver info: driver.version: HtmlUnitDriver 
at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElementById(HtmlUnitDriver.java:685) 
at org.openqa.selenium.By$ById.findElement(By.java:210) 
at org.openqa.selenium.htmlunit.HtmlUnitDriver$5.call(HtmlUnitDriver.java:1222) 
at org.openqa.selenium.htmlunit.HtmlUnitDriver$5.call(HtmlUnitDriver.java:1) 
at org.openqa.selenium.htmlunit.HtmlUnitDriver.implicitlyWaitFor(HtmlUnitDriver.java:975) 
at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:1219) 
at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:396) 
at assignment.YahooRegistrationTest.testGender(YahooRegistrationTest.java:30) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
at java.lang.reflect.Method.invoke(Unknown Source) 
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) 
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) 
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) 
at org.junit.experimental.theories.Theories$TheoryAnchor$2.evaluate(Theories.java:167) 
at org.junit.experimental.theories.Theories$TheoryAnchor$1$1.evaluate(Theories.java:133) 
... 20 more 

但是,我不知道爲什麼發生這種情況爲「性別」當您查看源確實存在。我現在正在使用HtmlUnitDriver。

+1

你設置'driver.setJavascriptEnabled(真) '? – jeha

+0

您是否傾銷了您回來的HTML,或成功訪問了頁面上的其他元素,作爲完整性檢查以確保您正在使用您認爲自己的DOM? –

+0

我無法訪問我嘗試過的任何其他元素(或我相信)。此外,我正在使用的版本(2.6.0)上沒有關於driver.setJavascriptEnabled(true)的選項。最後,你如何轉儲你回來的HTML? – Confused

回答

2

啓用JavaScript:

driver.setJavascriptEnabled(true); 
1

@koteswararo:打開網址改成像以前一樣嘗試打印網頁的源文件的無法訪問的網址:

 System.out.println(driver.getPageSource()); 
     driver.get("https://edit.yahoo.com/registration");