HtmlUnitDriver似乎没有正在加载页面 [英] HtmlUnitDriver does not appear to be loading page

查看:238
本文介绍了HtmlUnitDriver似乎没有正在加载页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to get HtmlUnitDriver to work in my development environment. As a beginner, I've tried implementing the example from the following page using the latest selenium server jar: http://code.google.com/p/selenium/wiki/GettingStarted

不幸的是,每当我尝试运行该程序时,都会出现以下异常:

Unfortunately, whenever I try to run this program, I get the following exception:

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)

我尝试修改我的代码以合并此处实现的修补程序:

I've tried modifying my code to incorporate the fixes implemented here:

HtmlUnitDriver在获取URL时会引起问题

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

I've tried getting the URL of the page using driver.getCurrentUrl() after the call to driver.get("http://www.google.com"), but the string that is returned is about:blank.

如果我使用FirefoxDriver运行此示例中的类似代码,则绝对可以运行,但是为了满足要求,我需要我的脚本使用硒进行无头运行(如果可以使用特定的BrowserVersion运行就可以了,只要它可以没有头).

Similar code in this example would definitely work if I were to run it using FirefoxDriver, but to meet requirements, I need my script to run headless with selenium (it is okay if it is run with a specific BrowserVersion so long as it is headless).

任何帮助将不胜感激.

更新:

这是我现在正在尝试运行的代码.我只希望看到我可以使HtmlUnitDriver可以像在Google中输入搜索查询一样简单.

This is the code I'm trying to run right now. I just want to see that I can get HtmlUnitDriver to work with something as simple as entering a search query into 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());

}
}

更新2:

这个问题很奇怪.我已经在家里的计算机上尝试过相同的代码,并且在将BrowserVersion设置为Firefox或Internet Explorer时,它可以很好地工作.尽管我仍然不知道为什么会这样,但这个问题肯定是由我工作场所中的计算机配置引起的.

This issue is very bizarre. I've tried the same code on my computer at home and it works perfectly fine with BrowserVersion set to Firefox or Internet Explorer. This issue is definitely being caused by the computer configuration at my workplace, although I still have no idea why this is happening.

推荐答案

我会尝试一下:

如果是网络问题,请下载此工具以检查您正在连接的端口是否正在使用: http://technet.microsoft.com/en-us/sysinternals/bb897437.aspx

If it is a network problem, please download this tool to check if the port your are connecting to is in use: http://technet.microsoft.com/en-us/sysinternals/bb897437.aspx

在这种情况下非常方便.

Very handy for these kind of situations.

要进行进一步的调试,请尝试读出源代码. 将此行放在第一个元素选择器之前和页面加载之后.

For further debugging, try reading out the source. Put this line before the first element selector and after the page loaded.:

System.out.println(driver.getPageSource());

我想知道元素是否在那里.

I wonder if the element is there..

这篇关于HtmlUnitDriver似乎没有正在加载页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆