user3593320
user3593320

Reputation: 53

How to get an Image by Xpath in HtmlUnit?

I am trying to get an Image from an XPath in HtmlUnit, but the way I try it is not working for me.

Here is the Element I try to get:

<div id="image" style="width: 300px; height: 57px;"><img id="image_id" alt="Bild" height="57" width="300" src="snip"></div>

And here is my Code:

final List<?> divs = form.getByXPath("//div[@id='recaptcha_widget_div']//div[@id='recaptcha_image']");
HtmlImage img = (HtmlImage) imgs.get(0);

I know I cant cast a Division to a HtmlImage, but I dont know how to get the Image from the Division.

Upvotes: 1

Views: 1459

Answers (1)

Adrien
Adrien

Reputation: 375

The element has a img tag. you should take its XPath with browser and use in HtmlUnit getFirstByXPath and put it in HtmlImage:

HtmlImage img = myPage.getFirstByXPath("//img[@id='image_id'])");

But if you have an element id, I recommend you using .getElementById(String id) method.

Upvotes: 1

Related Questions