HtmlUnitDriver似乎没有加载页面

Jus*_*tin 5 java selenium htmlunit-driver

我正在尝试让HtmlUnitDriver在我的开发环境中工作.作为初学者,我尝试使用最新的selenium服务器jar实现以下页面中的示例:http: //code.google.com/p/selenium/wiki/GettingStarted

不幸的是,每当我尝试运行此程序时,我都会遇到以下异常:

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element with name: q
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.35.0', revision: 'c916b9d', time: '2013-08-12 15:42:01'
System info: os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.6.0_16'
Driver info: driver.version: HtmlUnitDriver
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElementByName(HtmlUnitDriver.java:853)
    at org.openqa.selenium.By$ByName.findElement(By.java:292)
    at org.openqa.selenium.htmlunit.HtmlUnitDriver$5.call(HtmlUnitDriver.java:1404)
    at org.openqa.selenium.htmlunit.HtmlUnitDriver$5.call(HtmlUnitDriver.java:1)
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.implicitlyWaitFor(HtmlUnitDriver.java:1094)
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:1401)
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:419)
    at Justin.Main.main(Main.java:30)
Run Code Online (Sandbox Code Playgroud)

我已经尝试修改我的代码以包含此处实现的修复:

获取网址时,HtmlUnitDriver会导致问题

我已经尝试driver.getCurrentUrl()在调用后获取页面的URL driver.get("http://www.google.com"),但返回的字符串是about:blank.

如果我使用FirefoxDriver运行它,这个例子中的类似代码肯定会有用,但是为了满足要求,我需要我的脚本用selenium运行无头(如果它是使用特定的BrowserVersion运行,只要它是无头的就可以了) .

任何帮助将不胜感激.

更新:

这是我正在尝试运行的代码.我只想看到我可以让HtmlUnitDriver使用像向Google输入搜索查询一样简单的工作.

package Justin;


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;


public class Main {
public static void main(final String[] args) {
    // Create a new instance of the html unit driver
    // Notice that the remainder of the code relies on the interface,
    // not the implementation.
    final WebDriver driver = new HtmlUnitDriver();
    ((HtmlUnitDriver)driver).setJavascriptEnabled(true);

    // And now use this to visit Google
    driver.get("http://www.google.com");

    try {
        Thread.sleep(30000);
    } catch (final InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    // Find the text input element by its name
    final WebElement element = driver.findElement(By.name("q"));

    // Enter something to search for
    element.sendKeys("Cheese!");

    // Now submit the form. WebDriver will find the form for us from the element
    element.submit();

    // Check the title of the page
    System.out.println("Page title is: " + driver.getTitle());

}
}
Run Code Online (Sandbox Code Playgroud)

更新2:

这个问题非常奇怪.我在家里的计算机上尝试了相同的代码,并且将BrowserVersion设置为Firefox或Internet Explorer时效果非常好.这个问题肯定是由我工作场所的计算机配置造成的,虽然我仍然不知道为什么会这样.

小智 1

在给出的示例代码中,它假设旧的 Google 页面存在,其中包含 name=q 的搜索字段。

该页面不再以这种方式标记 - 尝试更改driver.findElement(By.name("q"))driver.findElement(By.cssSelector("input[id=gbqfq]")并查看是否可以解决您的问题 - 至少一旦您加载页面,它应该能够输入到搜索栏。

如果您driver.getCurrentUrl()在尝试获取页面后立即调用,它可能尚未完全加载,而是返回 about:blank。

如果这不起作用,我们可以继续进行故障排除 - 使用无头浏览器很难看到实际发生的情况,因此您可能需要暂时切换到 FirefoxDriver 以准确地可视化发生的情况。