[HtmlUnit] [htmlunit:bugs] #2020 Unable to save image locally from HtmlImage object
RBRi via HtmlUnit-develop <[email protected]> Sun, 21 Jul 2019 12:16:44 -0000
| Newsgroups | gmane.comp.java.htmlunit.devel |
|---|---|
| Message-ID | </p/htmlunit/bugs/2020/2274e3861c06657fa8c30f1060a1cdb7c8eedee0.bugs@htmlunit.p.sourceforge.net> |
This is a multi-part message in MIME format.
--===============6008519502349788881==
Content-Type: multipart/related;
boundary="===============3396059054801100928=="
This is a multi-part message in MIME format.
--===============3396059054801100928==
Content-Type: multipart/alternative;
boundary="===============1393905951637283159=="
MIME-Version: 1.0
--===============1393905951637283159==
MIME-Version: 1.0
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
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 [email protected] 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.
--===============1393905951637283159==
MIME-Version: 1.0
Content-Type: text/html; charset="us-ascii"
Content-Transfer-Encoding: 7bit
<div class="markdown_content"><p>Using the latest version (the latest snapshot build) i was able to download the image using this code:</p>
<div class="codehilite"><pre><span></span> <span class="nt">try</span> <span class="o">(</span><span class="nt">final</span> <span class="nt">WebClient</span> <span class="nt">webClient</span> <span class="o">=</span> <span class="nt">new</span> <span class="nt">WebClient</span><span class="o">(</span><span class="nt">BrowserVersion</span><span class="p">.</span><span class="nc">FIREFOX_60</span><span class="o">))</span> <span class="p">{</span>
<span class="err">WebClientOptions</span> <span class="err">webClientOption</span> <span class="err">=</span> <span class="err">webClient.getOptions()</span><span class="p">;</span>
<span class="err">//</span> <span class="err">ignore</span> <span class="err">js</span> <span class="err">errors</span> <span class="err">and</span> <span class="err">proceed</span>
<span class="err">//</span> <span class="err">even</span> <span class="err">in</span> <span class="err">real</span> <span class="err">browsers</span> <span class="err">this</span> <span class="err">page</span> <span class="err">produces</span> <span class="err">millions</span> <span class="err">of</span> <span class="err">warnings</span>
<span class="err">webClientOption.setThrowExceptionOnFailingStatusCode(false)</span><span class="p">;</span>
<span class="err">String</span> <span class="err">searchUrl</span> <span class="err">=</span> <span class="err">"</span><span class="n">http</span><span class="p">:</span><span class="o">//</span><span class="n">ceojammukashmir</span><span class="o">.</span><span class="n">nic</span><span class="o">.</span><span class="n">in</span><span class="o">/</span><span class="n">NameSearch</span><span class="o">/</span><span class="n">main</span><span class="o">.</span><span class="n">aspx</span><span class="s2">";</span>
<span class="s2"> webClient.getPage(searchUrl);</span>
<span class="s2"> // wait to let the (async) js do what has to be done</span>
<span class="s2"> webClient.waitForBackgroundJavaScript(20_000);</span>
<span class="s2"> // looks like the js updates the window content</span>
<span class="s2"> // do not use the outdated page we got by getPage because we have to work with the current content</span>
<span class="s2"> HtmlPage currentPage = (HtmlPage) webClient.getCurrentWindow().getEnclosedPage();</span>
<span class="s2"> HtmlImage image = (HtmlImage) currentPage.getByXPath("</span><span class="o">//</span><span class="n">img</span><span class="cp">[</span><span class="p">@</span><span class="n">id</span><span class="o">=</span><span class="s1">'captchaDetailImg'</span> <span class="ow">and</span> <span class="p">@</span><span class="n">class</span><span class="o">=</span><span class="s1">'bordered-desktop'</span><span class="cp">]</span><span class="s2">").get(0);</span>
<span class="s2"> // looks promising so far</span>
<span class="s2"> System.out.println(image.asXml());</span>
<span class="s2"> File captchaFile = new File("</span><span class="n">C</span><span class="o">:</span><span class="err">\\</span><span class="n">TEMP</span><span class="err">\\</span><span class="n">test</span><span class="o">.</span><span class="n">jpg</span><span class="err">"</span><span class="p">);</span>
<span class="err">image.saveAs(captchaFile)</span><span class="p">;</span>
<span class="err">//</span> <span class="err">yep</span> <span class="err">there</span> <span class="err">is</span> <span class="err">the</span> <span class="err">file</span>
<span class="err">//</span> <span class="err">have</span> <span class="err">fun</span> <span class="err">using</span> <span class="err">HtmlUnit</span>
<span class="p">}</span>
</pre></div>
<p>Hope you get the same results.<br/>
If you like you can follow the development on <a href="https://twitter.com/HtmlUnit." rel="nofollow">https://twitter.com/HtmlUnit.</a></p>
<p>If you have more questions/issues please us github (https://github.com/HtmlUnit/htmlunit)</p>
<p>Thanks for using HtmlUnit</p>
<hr/>
<p><strong> <a class="alink" href="https://sourceforge.net/p/htmlunit/bugs/2020/">[bugs:#2020]</a> Unable to save image locally from HtmlImage object</strong></p>
<p><strong>Status:</strong> open<br/>
<strong>Group:</strong> 2.35.0<br/>
<strong>Created:</strong> Thu Jul 11, 2019 04:58 AM UTC by raman palikala<br/>
<strong>Last Updated:</strong> Fri Jul 19, 2019 12:30 PM UTC<br/>
<strong>Owner:</strong> nobody<br/>
<strong>Attachments:</strong></p>
<ul>
<li><a class="" href="https://sourceforge.net/p/htmlunit/bugs/2020/attachment/Image_Content_As_JSON.txt">Image_Content_As_JSON.txt</a> (2.0 kB; text/plain)</li>
</ul>
<p>Hi Team,</p>
<p>I have just started exploring HtmlUnit.<br/>
I am using htmlunit v2.21.<br/>
The problem is I am unable to save image from HtmlImage object.</p>
<p>Below is my sample code :</p>
<p>HtmlImage image = (HtmlImage) page<br/>
.getByXPath("//img<span>[@id='captchaDetailImg' and @class='bordered-desktop']</span>")<br/>
.get(0);<br/>
File captchaFile = new File(newFilePathOfImage);<br/>
image.saveAs(captchaFile);</p>
<p>Here, in the above code when <strong>image.saveAs(captchaFile);</strong><br/>
is executed it is throwing the following error:<br/>
<strong>java.io.IOException: No image response available (src=)</strong></p>
<p>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.<br/>
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.</p>
<p>Could you please let me know what I have to do to construct the image from the json content<br/>
OR is there any way to get the image directly or save it locally from the HtmlImage object in this <br/>
scenario?<br/>
Awaiting for your response.</p>
<p>Thanks and regards,<br/>
Raman Palikala.</p>
<hr/>
<p>Sent from sourceforge.net because [email protected] is subscribed to <a href="https://sourceforge.net/p/htmlunit/bugs/">https://sourceforge.net/p/htmlunit/bugs/</a></p>
<p>To unsubscribe from further messages, a project admin can change settings at <a href="https://sourceforge.net/p/htmlunit/admin/bugs/options.">https://sourceforge.net/p/htmlunit/admin/bugs/options.</a> Or, if this is a mailing list, you can unsubscribe from the mailing list.</p></div>
--===============1393905951637283159==--
--===============3396059054801100928==--
--===============6008519502349788881==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
--===============6008519502349788881==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
_______________________________________________
HtmlUnit-develop mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/htmlunit-develop
--===============6008519502349788881==--