Search in sources :

Example 1 with SilentCssErrorHandler

use of com.gargoylesoftware.htmlunit.SilentCssErrorHandler in project yyl_example by Relucent.

the class HtmlUnitTest2 method main.

public static void main(String[] args) throws Exception {
    WebClient webClient = new WebClient(BrowserVersion.FIREFOX_68);
    webClient.setCssErrorHandler(new SilentCssErrorHandler());
    webClient.setAjaxController(new NicelyResynchronizingAjaxController());
    webClient.getOptions().setCssEnabled(true);
    webClient.getOptions().setRedirectEnabled(false);
    webClient.getOptions().setAppletEnabled(false);
    webClient.getOptions().setJavaScriptEnabled(true);
    webClient.getOptions().setPopupBlockerEnabled(true);
    webClient.getOptions().setTimeout(10000);
    // JS运行错误时,是否抛出异常
    webClient.getOptions().setThrowExceptionOnScriptError(false);
    HtmlPage page = webClient.getPage("http://huaban.com/favorite/home/");
    System.out.println(page.asXml());
    webClient.close();
}
Also used : HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) NicelyResynchronizingAjaxController(com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController) SilentCssErrorHandler(com.gargoylesoftware.htmlunit.SilentCssErrorHandler) WebClient(com.gargoylesoftware.htmlunit.WebClient)

Example 2 with SilentCssErrorHandler

use of com.gargoylesoftware.htmlunit.SilentCssErrorHandler in project ats-framework by Axway.

the class HiddenBrowserDriver method fixHtmlUnitBehaviour.

/**
 * Fixing refresh handler to skip Refresh meta tags
 * Allowing connections to any host, regardless of whether they have valid certificates or not
 * Fixing JSESSIONID cookie value
 * Some applications expect double quotes in the beginning and at the end of the JSESSIONID cookie value
 */
private void fixHtmlUnitBehaviour() {
    Field webClientField = null;
    boolean fieldAccessibleState = false;
    try {
        TargetLocator targetLocator = webDriver.switchTo();
        webClientField = targetLocator.getClass().getDeclaringClass().getDeclaredField("webClient");
        fieldAccessibleState = webClientField.isAccessible();
        webClientField.setAccessible(true);
        final WebClient webClient = (WebClient) webClientField.get(targetLocator.defaultContent());
        // Allowing connections to any host, regardless of whether they have valid certificates or not
        webClient.getOptions().setUseInsecureSSL(true);
        // Set Http connection timeout (in milliseconds). The default value is 90 seconds, because in Firefox >= 16
        // the "network.http.connection-timeout" property is 90. But this value is not enough for some cases.
        // NOTE: use 0 for infinite timeout
        webClient.getOptions().setTimeout(5 * 60 * 1000);
        webClient.getOptions().setRedirectEnabled(true);
        webClient.getOptions().setJavaScriptEnabled(true);
        webClient.getOptions().setThrowExceptionOnScriptError(true);
        webClient.getOptions().setPrintContentOnFailingStatusCode(true);
        // Hide CSS Warnings
        webClient.setCssErrorHandler(new SilentCssErrorHandler());
        // Suppress warnings like: "Expected content type ... but got ..."
        webClient.setIncorrectnessListener(new IncorrectnessListener() {

            // private final Log log = LogFactory.getLog( this.getClass() );
            @Override
            public void notify(final String message, final Object origin) {
            // log.warn( message );
            }
        });
        if (!Boolean.parseBoolean(System.getProperty(ALLOW_META_REFRESH_TAG))) {
            /*
                 * Fix for refresh meta tags eg. "<meta http-equiv="refresh" content="300">"
                 * The default refresh handler is with Thread.sleep(refreshSecondsFromMetaTag) in the main thread!!!
                     *
                     * Maybe we should check and test this handler: webClient.setRefreshHandler( new ThreadedRefreshHandler() );
                 */
            webClient.setRefreshHandler(new RefreshHandler() {

                @Override
                public void handleRefresh(Page page, URL url, int seconds) throws IOException {
                }
            });
        }
        /*
             * Fix JSessionId
             */
        // WebConnectionWrapper constructs a WebConnection object wrapping the connection of the WebClient
        // and places itself (in the constructor) as connection of the WebClient.
        new WebConnectionWrapper(webClient) {

            public WebResponse getResponse(WebRequest request) throws IOException {
                Cookie jsCookie = webClient.getCookieManager().getCookie("JSESSIONID");
                if (jsCookie != null && (!jsCookie.getValue().startsWith("\"") && !jsCookie.getValue().endsWith("\""))) {
                    Cookie newCookie = new Cookie(jsCookie.getDomain(), jsCookie.getName(), "\"" + jsCookie.getValue() + "\"", jsCookie.getPath(), jsCookie.getExpires(), jsCookie.isSecure());
                    webClient.getCookieManager().removeCookie(jsCookie);
                    webClient.getCookieManager().addCookie(newCookie);
                }
                return super.getResponse(request);
            }
        };
    } catch (Exception e) {
        throw new SeleniumOperationException("Error retrieving internal Selenium web client", e);
    } finally {
        if (webClientField != null) {
            webClientField.setAccessible(fieldAccessibleState);
        }
    }
}
Also used : Cookie(com.gargoylesoftware.htmlunit.util.Cookie) Page(com.gargoylesoftware.htmlunit.Page) IOException(java.io.IOException) SeleniumOperationException(com.axway.ats.uiengine.exceptions.SeleniumOperationException) WebClient(com.gargoylesoftware.htmlunit.WebClient) IncorrectnessListener(com.gargoylesoftware.htmlunit.IncorrectnessListener) URL(java.net.URL) SeleniumOperationException(com.axway.ats.uiengine.exceptions.SeleniumOperationException) IOException(java.io.IOException) Field(java.lang.reflect.Field) WebRequest(com.gargoylesoftware.htmlunit.WebRequest) TargetLocator(org.openqa.selenium.WebDriver.TargetLocator) SilentCssErrorHandler(com.gargoylesoftware.htmlunit.SilentCssErrorHandler) RefreshHandler(com.gargoylesoftware.htmlunit.RefreshHandler) WebConnectionWrapper(com.gargoylesoftware.htmlunit.util.WebConnectionWrapper)

Example 3 with SilentCssErrorHandler

use of com.gargoylesoftware.htmlunit.SilentCssErrorHandler in project yyl_example by Relucent.

the class HtmlUnitTest3 method main.

public static void main(String[] args) throws Exception {
    WebClient webClient = new WebClient(BrowserVersion.FIREFOX_68);
    try {
        webClient.setCssErrorHandler(new SilentCssErrorHandler());
        webClient.setAjaxController(new NicelyResynchronizingAjaxController());
        webClient.getOptions().setCssEnabled(true);
        webClient.getOptions().setRedirectEnabled(false);
        webClient.getOptions().setAppletEnabled(false);
        webClient.getOptions().setJavaScriptEnabled(true);
        webClient.getOptions().setPopupBlockerEnabled(true);
        webClient.getOptions().setTimeout(10000);
        // JS运行错误时,是否抛出异常
        webClient.getOptions().setThrowExceptionOnScriptError(false);
        // webClient.waitForBackgroundJavaScript(10 * 1000);
        HtmlPage page = webClient.getPage("https://www.baidu.com/");
        WebWindow webWindow = page.getEnclosingWindow();
        System.out.println("# 等待页面加载");
        waitFor(() -> {
            DomElement input = page.getElementById("kw");
            return input instanceof HtmlInput;
        });
        System.out.println("# 文本框输入 htmlunit ");
        HtmlInput kw = (HtmlInput) page.getElementById("kw");
        kw.setAttribute("value", "htmlunit");
        System.out.println("# 触发回车事件");
        Thread.sleep(1000);
        // Enter
        kw.type(13);
        System.out.println("# 等待页面跳转");
        Thread.sleep(1000);
        HtmlPage page2 = (HtmlPage) webWindow.getEnclosedPage();
        System.out.println(page2.getUrl());
        DomNodeList<DomNode> nodes = page2.querySelectorAll(".result.c-container h3 a");
        System.out.println("# 输出结果");
        for (DomNode node : nodes) {
            System.out.println(node.asText());
        }
    } finally {
        webClient.close();
    }
}
Also used : DomNode(com.gargoylesoftware.htmlunit.html.DomNode) DomElement(com.gargoylesoftware.htmlunit.html.DomElement) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) NicelyResynchronizingAjaxController(com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController) SilentCssErrorHandler(com.gargoylesoftware.htmlunit.SilentCssErrorHandler) WebClient(com.gargoylesoftware.htmlunit.WebClient) HtmlInput(com.gargoylesoftware.htmlunit.html.HtmlInput) WebWindow(com.gargoylesoftware.htmlunit.WebWindow)

Aggregations

SilentCssErrorHandler (com.gargoylesoftware.htmlunit.SilentCssErrorHandler)3 WebClient (com.gargoylesoftware.htmlunit.WebClient)3 NicelyResynchronizingAjaxController (com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController)2 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)2 SeleniumOperationException (com.axway.ats.uiengine.exceptions.SeleniumOperationException)1 IncorrectnessListener (com.gargoylesoftware.htmlunit.IncorrectnessListener)1 Page (com.gargoylesoftware.htmlunit.Page)1 RefreshHandler (com.gargoylesoftware.htmlunit.RefreshHandler)1 WebRequest (com.gargoylesoftware.htmlunit.WebRequest)1 WebWindow (com.gargoylesoftware.htmlunit.WebWindow)1 DomElement (com.gargoylesoftware.htmlunit.html.DomElement)1 DomNode (com.gargoylesoftware.htmlunit.html.DomNode)1 HtmlInput (com.gargoylesoftware.htmlunit.html.HtmlInput)1 Cookie (com.gargoylesoftware.htmlunit.util.Cookie)1 WebConnectionWrapper (com.gargoylesoftware.htmlunit.util.WebConnectionWrapper)1 IOException (java.io.IOException)1 Field (java.lang.reflect.Field)1 URL (java.net.URL)1 TargetLocator (org.openqa.selenium.WebDriver.TargetLocator)1