且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用HtmlUnit登录到gmail

更新时间:2023-12-04 11:00:58

内置浏览器可能无法正确解析Javascript.

It's possible that the built-in browser can't parse the Javascript correctly.

尝试模拟其他浏览器.来自文档:

Try emulating a different browser. From the docs:

通常,您将需要模拟特定的浏览器.这是通过将com.gargoylesoftware.htmlunit.BrowserVersion传递到WebClient构造函数中来完成的.已为某些常见的浏览器提供了常量,但您可以通过实例化BrowserVersion来创建自己的特定版本.

Often you will want to simulate a specific browser. This is done by passing a com.gargoylesoftware.htmlunit.BrowserVersion into the WebClient constructor. Constants have been provided for some common browsers but you can create your own specific version by instantiating a BrowserVersion.

@Test
public void homePage_Firefox() throws Exception {
    final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_17);
    final HtmlPage page = webClient.getPage("http://htmlunit.sourceforge.net");
    Assert.assertEquals("HtmlUnit - Welcome to HtmlUnit", page.getTitleText());

    webClient.closeAllWindows();
}

此代码段模拟Firefox v17.您可以在这些Javadocs中看到完整的受支持的浏览器列表.今天,这些是:

This snippet emulates Firefox v17. You can see the full list of supported, emulatable browsers in these javadocs. Today, these are:

static BrowserVersion   CHROME
static BrowserVersion   FIREFOX_17
static BrowserVersion   FIREFOX_24
static BrowserVersion   INTERNET_EXPLORER_11
static BrowserVersion   INTERNET_EXPLORER_8
static BrowserVersion   INTERNET_EXPLORER_9