无法使用HtmlUnitDriver点击网页链接但是Same正在使用FirefoxDriver。为什么?

时间:2011-03-24 21:20:25

标签: firefox selenium automation htmlunit webdriver

当我尝试使用FirefoxDriver下面的代码行点击链接时,它运行正常。

WebDriver driver = new FirefoxDriver();
driver.findElement(By.linkText("More...")).click();

但是当我使用HtmlUnitDriver时,我就会遇到异常。

org.openqa.selenium.NoSuchElementException: No link found with text: More...
System info: os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.6.0_24'
Driver info: driver.version: HtmlUnitDriver
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElementByLinkText(HtmlUnitDriver.java:630)
    at org.openqa.selenium.By$2.findElement(By.java:93)
    at org.openqa.selenium.htmlunit.HtmlUnitDriver$4.call(HtmlUnitDriver.java:1198)
    at org.openqa.selenium.htmlunit.HtmlUnitDriver$4.call(HtmlUnitDriver.java:1)
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.implicitlyWaitFor(HtmlUnitDriver.java:934)
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:1195)
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:377)
    at com.search.engine.BotwSearchEngine.isPageExists(BotwSearchEngine.java:154)
    at com.search.engine.BotwSearchEngine.searchCategories(BotwSearchEngine.java:82)
    at com.search.engine.BotwSearchEngine.main(BotwSearchEngine.java:189)

请帮我解决此问题。

1 个答案:

答案 0 :(得分:1)

链接是否可能在页面的初始加载中不存在,并随后使用JavaScript添加?如果是这样,您可能会在创建HTMLDriver实例时检查是否启用了JavaScript:WebDriver driver = new HTMLUnitDriver(true);

http://code.google.com/p/selenium/wiki/HtmlUnitDriver