找不到具有ID的元素,即使它存在

时间:2011-09-16 21:25:22

标签: java selenium

我想在this website上选择性别字段。我使用了以下代码:

    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。

2 个答案:

答案 0 :(得分:2)

启用JavaScript:

driver.setJavascriptEnabled(true);

答案 1 :(得分:1)

@koteswararo:在打开URl之前尝试打印页面源,看起来无法访问该URL:

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