Java HtmlUnitDriver 似乎没有加载页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19139151/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 14:27:37  来源:igfitidea点击:

HtmlUnitDriver does not appear to be loading page

javaseleniumhtmlunit-driver

提问by Justin

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

我试图让 HtmlUnitDriver 在我的开发环境中工作。作为初学者,我尝试使用最新的 selenium 服务器 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.call(HtmlUnitDriver.java:1404)
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.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 causes problems while getting an url

HtmlUnitDriver 在获取 url 时导致问题

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.

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

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).

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

Any help would be greatly appreciated.

任何帮助将不胜感激。

UPDATE:

更新:

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.

这是我现在正在尝试运行的代码。我只是想看看我可以让 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());

}
}

UPDATE 2:

更新 2:

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.

这个问题非常奇怪。我已经在家里的电脑上尝试了相同的代码,它在 BrowserVersion 设置为 Firefox 或 Internet Explorer 时运行得非常好。这个问题肯定是由我工作场所的计算机配置引起的,尽管我仍然不知道为什么会发生这种情况。

回答by JPierce

In the example code given, it assumes the older Google page exists which has the search field with name=q.

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

The page no longer is labelled that way - try changing driver.findElement(By.name("q"))to driver.findElement(By.cssSelector("input[id=gbqfq]")and see if that fixes your issue - at the very least it should be able to input to the search bar once you get the page loaded up.

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

If you called driver.getCurrentUrl()immediately after trying to get the page, it may not have loaded completely and instead returned about:blank.

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

If that doesn't work, we can keep troubleshooting - it's harder to see what's actually happening with a headless browser, so you may want to switch to a FirefoxDriver temporarily to visualize exactly what's happening.

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

回答by JPierce

Try using the Selenium IDEplugin with Firefox to see what it scrapes when it does it actions. You can export the code into java and then try to run that also. I would also step through the code on the both home and work computer to see if there are any differences. I have seen code work (when that element not found error happens) when being stepped through due to the DOM having time to fully load. Furthermore, try to instantiate and actual browser so you can visuallly see whats happening.

尝试在Firefox 中使用Selenium IDE插件,看看它在执行操作时会抓取什么。您可以将代码导出到 java 中,然后也尝试运行它。我还会逐步检查家庭和工作计算机上的代码,看看是否有任何差异。由于 DOM 有时间完全加载,我已经看到代码工作(当该元素未找到错误发生时)。此外,尝试实例化实际浏览器,以便您可以直观地看到发生了什么。

Also, verify version of all software your using between home and workstation.

此外,验证您在家庭和工作站之间使用的所有软件的版本。

回答by HerrWalter

Ill try to give it a shot:

我会尝试试一试:

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

如果是网络问题,请下载此工具以检查您连接的端口是否正在使用: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..

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

回答by Bart Wojtala

1)Try to add

1)尝试添加

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

before

final WebElement element = driver.findElement(By.name("q"));

2)or try

2)或尝试

 try {
      WebElement element = driver.findElement(By.name("q"));
    } catch (Exception e) {
      Thread.sleep(1000);  
    }

回答by NoviceTechie

Try using

尝试使用

HtmlUnitDriver driver = new HtmlUnitDriver();

Can you check if the page is loaded by checking

您可以通过检查来检查页面是否已加载

String pageTitle = driver.getTitle();

回答by xvan

If you require headless selenium, and firefox works with your test, try using the phantomjs webdriver.

如果您需要无头 selenium,并且 firefox 可用于您的测试,请尝试使用 phantomjs webdriver。