Downloading javascript image with HtmlUnit

时光毁灭记忆、已成空白 提交于 2020-05-14 09:39:11

问题


How do I go about downloading the image generated at Leaflet easyPrint button using HtmlUnit?

I am trying it like this:

public static void main(String[] args) {
        try{
            WebClient webClient = new WebClient();
            HtmlPage test = webClient.getPage("http://rowanwins.github.io/leaflet-easyPrint/");
            webClient.waitForBackgroundJavaScript(5000);

            final DomElement button = test.getFirstByXPath("/html/body/button");
            final InputStream image = button.click().getWebResponse().getContentAsStream();
            System.out.println(image);

            File file = new File("/home/josue/Basis/STS4/map.png");
            copyInputStreamToFile(image, file);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

private static void copyInputStreamToFile(InputStream inputStream, File file) 
        throws IOException {

        try (FileOutputStream outputStream = new FileOutputStream(file)) {

            int read;
            byte[] bytes = new byte[1024];

            while ((read = inputStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, read);
            }
        }
    }

And getting a blank PNG file with about 3Kb. What is the proper way to get it working?

EDIT: The reason I want to accomplish it, is to get a simple alternative for Google Maps static API, which I currently have deployed in a running project.


回答1:


I couldn't get it to work with HtmlUnit, but got the expected result using Selenium, in case someone else is interested in this feature:

public class PG1 {


    static WebDriver driver;

    public static void main(String[] args) throws InterruptedException {
        System.setProperty("webdriver.gecko.driver","/Users/home/Downloads/geckodriver");
        FirefoxOptions fxProfile = new FirefoxOptions();
        fxProfile.setHeadless(true);
        fxProfile.addPreference("browser.download.folderList",2);
        fxProfile.addPreference("browser.download.manager.showWhenStarting",false);
        fxProfile.addPreference("browser.download.dir","/Users/home/Downloads/");
        fxProfile.addPreference("browser.helperApps.neverAsk.saveToDisk","image/png");
        driver = new FirefoxDriver(fxProfile);
        String baseUrl = "http://rowanwins.github.io/leaflet-easyPrint/";
        driver.get(baseUrl);
        while(checkForPresenceOfElementByXpath("")) {
        }
        System.out.println("Loaded");
        driver.findElement(By.xpath("/html/body/button")).click();
        while(checkForPresenceOfElementByXpath("")) {
        }
        System.out.println("Downloaded");
        driver.close();
    }

    public static boolean checkForPresenceOfElementByXpath(String xpath){
        try{
            new WebDriverWait(driver, 5).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("(xpath)[last()]")));
            return true;
        }catch(Exception e){
            return false;
        }
    }
}


来源:https://stackoverflow.com/questions/60099762/downloading-javascript-image-with-htmlunit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!