开发者

HTMLUnit Open Window showing google.com

I need some very simple help to open google.com in a browser from a Java desktop app.

Looking at using HTMLUnit and something like this:

import java.io.IOException;

import java.net.URL; import java.util.List; import com.gargoylesoftware.htmlunit.WebWindow; import com.gargoylesoftware.htmlunit.BrowserVersion; import com.gargoylesoftware.htmlunit.Page; import com.gargoylesoftware.htmlunit.RefreshHandler; import com.gargoylesoftware.htmlunit.WebClient; import com.gargoylesoftware.htmlunit.html.HtmlAnchor; import com.gargoylesoftware.htmlunit.html.HtmlForm; import com.gargoylesoftware.htmlunit.html.HtmlPage; import com.gargoylesoftware.htmlunit.html.HtmlTable; import com.gargoylesoftware.htmlunit.html.HtmlTableRow;

public class HTMLUnit {

public static void main(String[] args) throws Exception {

// Create and initialize WebClient object WebClient webClient = new WebClient(BrowserVersion.INTERNET_EXPLORER_8);

        webClient.setThrowExceptionOnScriptError(false);
 webClient.setRefreshHandler(new RefreshHandler() {

public void handleRefresh(Page page, URL url, int arg) throws IOException { System.out.println("handleRefresh"); }

 });

        Page NewGooglePage = webClient.openWindow(new URL("http开发者_运维百科://www.google.com"), "GoogleWindow").getEnclosedPage();

When running this file in NetBeans should I get a window to pop?


No,

HtmlUnit is a "headless browser". It means every things you do with HtmlUnit are not visible.

Instead I suggest you try WebDriver/Selenium 2 (http://seleniumhq.org/docs/09_webdriver.html). With WebDriver you can remote control browser like Firefox or IE.

Something like:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.RenderedWebElement;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class GoogleSuggest {
    public static void main(String[] args) throws Exception {
        // The Firefox driver supports javascript
        WebDriver driver = new FirefoxDriver();

        // Go to the Google Suggest home page
        driver.get("http://www.google.com/webhp?complete=1&hl=en");

        // Enter the query string "Cheese"
        WebElement query = driver.findElement(By.name("q"));
        query.sendKeys("Cheese");

     }
}


...this may be related to the parameters send in the request. I compared the ones send by HTMLUnit and the original browsers. There are differences. By the way you can add the missing request paramters in HmlUnit.


Interestingly this code worked, I changed to the IE browser, seems something is up with FireFox.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;



public class KeywordTool {
    private static final By By = null;

    public static void main(String[] args) throws Exception {
        // The Firefox driver supports javascript
        WebDriver driver = new InternetExplorerDriver();

        // Go to the Google Suggest home page
        driver.get("http://www.google.com");

        // Enter the query string "Cheese"
        WebElement query = driver.findElement(By.name("q"));
        query.sendKeys("Cheese");

     }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜