ly641921791 · GitHub

Executing the following code will cause a ClassCastException

        try (final WebClient webClient = new WebClient()) {
            webClient.getOptions().setCssEnabled(false);
            webClient.getOptions().setJavaScriptEnabled(false);
            webClient.getOptions().setMaxInMemory(500);
            HtmlPage page = webClient.getPage("http://www.shanghai.gov.cn/nw2/nw2314/nw2319/nw41893/index8.html");
            HtmlAnchor a;
            // Get this tag directly to execute openLinkInNewWindow without problems
            a = page.getFirstByXPath("//div[@class='container']/div/div/ul[@class='uli14 pageList']/li[5]/a");
            a.openLinkInNewWindow();
            a = page.getFirstByXPath("//div[@class='container']/div/div/ul[@class='uli14 pageList']/li[4]/a");
            a.openLinkInNewWindow();
            // If the previous tag is executed first, then executing this tag will cause a ClassCastException.
            // The exception message is com.gargoylesoftware.htmlunit.UnexpectedPage cannot be cast to com.gargoylesoftware.htmlunit.html.HtmlPage
            a = page.getFirstByXPath("//div[@class='container']/div/div/ul[@class='uli14 pageList']/li[5]/a");
            a.openLinkInNewWindow();
        }

Read the original on github.com ↗