Download file using HtmlUnit

后端 未结 6 1937
一生所求
一生所求 2021-02-05 23:17

I am trying to download xls file for a website. When I click the link to download the file, I get a javascript confirm box. I handle it like below

    ConfirmHan         


        
6条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-05 23:56

     final WebClient webClient = new WebClient(BrowserVersion.CHROME);
            webClient.getOptions().setTimeout(2000);
            webClient.getOptions().setThrowExceptionOnScriptError(false);
            webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
            webClient.waitForBackgroundJavaScript(2000);
    
            //get General page
            final HtmlPage page = webClient.getPage("http://your");
    
            //get Frame
            final HtmlPage frame = ((HtmlPage) 
            page.getFrameByName("Frame").getEnclosedPage());
    
            webClient.setConfirmHandler(new ConfirmHandler() {
                public boolean handleConfirm(Page page, String message) {
                    return true;
                }
            });
    
            //get element file
            final DomElement file = mainFrame.getElementByName("File");
    
            final InputStream xls =  file.click().getWebResponse().getContentAsStream();
    
            assertNotNull(xls);
        }
    

提交回复
热议问题