From: RBRi <rb...@us...> - 2019-07-21 12:16:45
|
Using the latest version (the latest snapshot build) i was able to download the image using this code: try (final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_60)) { WebClientOptions webClientOption = webClient.getOptions(); // ignore js errors and proceed // even in real browsers this page produces millions of warnings webClientOption.setThrowExceptionOnFailingStatusCode(false); String searchUrl = "http://ceojammukashmir.nic.in/NameSearch/main.aspx"; webClient.getPage(searchUrl); // wait to let the (async) js do what has to be done webClient.waitForBackgroundJavaScript(20_000); // looks like the js updates the window content // do not use the outdated page we got by getPage because we have to work with the current content HtmlPage currentPage = (HtmlPage) webClient.getCurrentWindow().getEnclosedPage(); HtmlImage image = (HtmlImage) currentPage.getByXPath("//img[@id='captchaDetailImg' and @class='bordered-desktop']").get(0); // looks promising so far System.out.println(image.asXml()); File captchaFile = new File("C:\\TEMP\\test.jpg"); image.saveAs(captchaFile); // yep there is the file // have fun using HtmlUnit } Hope you get the same results. If you like you can follow the development on https://twitter.com/HtmlUnit. If you have more questions/issues please us github (https://github.com/HtmlUnit/htmlunit) Thanks for using HtmlUnit --- ** [bugs:#2020] Unable to save image locally from HtmlImage object** **Status:** open **Group:** 2.35.0 **Created:** Thu Jul 11, 2019 04:58 AM UTC by raman palikala **Last Updated:** Fri Jul 19, 2019 12:30 PM UTC **Owner:** nobody **Attachments:** - [Image_Content_As_JSON.txt](https://sourceforge.net/p/htmlunit/bugs/2020/attachment/Image_Content_As_JSON.txt) (2.0 kB; text/plain) Hi Team, I have just started exploring HtmlUnit. I am using htmlunit v2.21. The problem is I am unable to save image from HtmlImage object. Below is my sample code : HtmlImage image = (HtmlImage) page .getByXPath("//img[@id='captchaDetailImg' and @class='bordered-desktop']") .get(0); File captchaFile = new File(newFilePathOfImage); image.saveAs(captchaFile); Here, in the above code when **image.saveAs(captchaFile);** is executed it is throwing the following error: **java.io.IOException: No image response available (src=)** When I observed the same html page in the browser to identify the cause of the error, I noticed that it is due to the image in the page not being transferred as a file, but the image did come as a response of type application/json. And that json content is of binary and I am not able to see the properties and their values in the json content. Please find the attached which has the response in json for the image. Could you please let me know what I have to do to construct the image from the json content OR is there any way to get the image directly or save it locally from the HtmlImage object in this scenario? Awaiting for your response. Thanks and regards, Raman Palikala. --- Sent from sourceforge.net because htm...@li... is subscribed to https://sourceforge.net/p/htmlunit/bugs/ To unsubscribe from further messages, a project admin can change settings at https://sourceforge.net/p/htmlunit/admin/bugs/options. Or, if this is a mailing list, you can unsubscribe from the mailing list. |