Cannot find element with ID even tho it exists
I would like to select the gender field on the this website. I used the following code:
driver.get("https://edit.yahoo.com/registration");
WebElement dropDownListBox = driver.findElement(By.id("gender"));
I get the following exception:
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
However, I am not sure why this is happening as "gender" does exist when you view the source. I am using HtmlUnitDriver for now.
Enable JavaScript:
driver.setJavascriptEnabled(true);
@koteswararo :try to print page source before opening URl look like your unable to access the URL:
System.out.println(driver.getPageSource());
driver.get("https://edit.yahoo.com/registration");
精彩评论