【问题标题】:How to get right page?如何获得正确的页面?
【发布时间】:2011-09-28 21:33:09
【问题描述】:

我使用 htmlunit 库来报废 Yellowpages.com 网站。我想输入搜索词并单击“查找”按钮。但在那之后我得到了 2 页:http://www.yellowpages.com/ny/sport?g=NY&q=Sporthttps://dealoftheday.yellowpages.com/join?ic=deal_pop-under_signup-v- 第一个是我想要的,第二个是弹出窗口。 我有这个代码:

public void getPage() throws FailingHttpStatusCodeException, MalformedURLException, IOException {
        WebClient webClient = new WebClient();
        page = webClient.getPage("http://www.yellowpages.com");
        HtmlTextInput searchInput = (HtmlTextInput) page.getElementById("search-terms");
        searchInput.setText("Law");

        HtmlSubmitInput button = (HtmlSubmitInput) page.getElementById("search-submit");
        page = button.click();
        System.out.println(page.getTitleText());

    }

此代码打印:

YP.com 每日特卖 - 加入

但我想打印第一页标题,即:

纽约体育 |纽约运动 - YP.com

如何获得第一页?

编辑:添加行 webClient.setPopupBlockerEnabled(true) 后,我收到了很多警告,之后出现了异常。这是控制台输出的一部分:

线程“主”中的异常 ======= 异常开始 ======== EcmaError: lineNumber=[56] column=[0] lineSource=[null] name=[TypeError] sourceName=[http://i2.ypcdn.com/webyp/javascripts/home_packaged.js?13455] message=[TypeError: 无法调用 null 的方法“blur” (http://i2.ypcdn.com/webyp/javascripts/home_packaged.js?13455#56)] com.gargoylesoftware.htmlunit.ScriptException:TypeError:无法调用 方法“模糊”为空 (http://i2.ypcdn.com/webyp/javascripts/home_packaged.js?13455#56)在 com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:601) 在 net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:537) 在 net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:538) 在 com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.callFunction(JavaScriptEngine.java:531) 在 com.gargoylesoftware.htmlunit.html.HtmlPage.executeJavaScriptFunctionIfPossible(HtmlPage.java:906) 在 com.gargoylesoftware.htmlunit.javascript.host.EventListenersContainer.executeEventListeners(EventListenersContainer.java:164) 在 com.gargoylesoftware.htmlunit.javascript.host.EventListenersContainer.executeBubblingListeners(EventListenersContainer.java:223) 在 com.gargoylesoftware.htmlunit.javascript.host.Node.fireEvent(Node.java:686) 在 com.gargoylesoftware.htmlunit.html.HtmlElement$2.run(HtmlElement.java:885) 在 net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:537) 在 net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:538) 在 com.gargoylesoftware.htmlunit.html.HtmlElement.fireEvent(HtmlElement.java:890) 在 com.gargoylesoftware.htmlunit.html.HtmlElement.fireEvent(HtmlElement.java:865) 在 com.gargoylesoftware.htmlunit.html.HtmlForm.submit(HtmlForm.java:108) 在 com.gargoylesoftware.htmlunit.html.HtmlSubmitInput.doClickAction(HtmlSubmitInput.java:77) 在 com.gargoylesoftware.htmlunit.html.HtmlElement.click(HtmlElement.java:1263) 在 com.gargoylesoftware.htmlunit.html.HtmlElement.click(HtmlElement.java:1214) 在 com.gargoylesoftware.htmlunit.html.HtmlElement.click(HtmlElement.java:1177) 在 YellowPages.getPage(YellowPages.java:39) 在 YellowPages.main(YellowPages.java:22) 原因: net.sourceforge.htmlunit.corejs.javascript.EcmaError: TypeError: 无法调用 null 的方法“模糊” (http://i2.ypcdn.com/webyp/javascripts/home_packaged.js?13455#56)在 net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3772) 在 net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3750) 在 net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.typeError(ScriptRuntime.java:3778)

【问题讨论】:

    标签: java htmlunit


    【解决方案1】:

    听起来像是 JS 错误。禁用 JS:

    webClient.setJavaScriptEnabled(false);
    

    然后呢?

    webClient.setThrowExceptionOnScriptError(false);
    

    如果使用 HtmlUnit 2.11+,请添加 webClient.getOptions()

    【讨论】:

    • 我已经找到了解决方案,谢谢。没错,webClient.setThrowExceptionOnScriptError(false),就是解决方案。
    【解决方案2】:

    你试过了吗

    webClient.setPopupBlockerEnabled(true)
    

    那么你应该只得到一页

    【讨论】:

      【解决方案3】:

      未测试,但我认为您可能会遍历 WebClient 的顶级窗口(使用 WebClient.getTopLevelWindows()),调用 getEnclosedPage() 并测试页面的标题文本是否是您要查找的。​​p >

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-06-20
        • 1970-01-01
        • 1970-01-01
        • 2021-12-25
        • 2014-03-05
        • 1970-01-01
        • 2019-10-05
        • 1970-01-01
        相关资源
        最近更新 更多