OrestesK
OrestesK

Reputation: 46

Java - Clicking on an image using HtmlUnit

I'm doing a personal project that involves getting data out of a website, I managed to make it automatically log in and all of that, but i have reached a point where i have to click on an image

img src="data:image/png;base64, {{sc.PhotoLocation}}" style="width: 75px; margin-top:3px;cursor:pointer" class="ng-cloak" ng-click="sc.selectPerson(sc.person_Guid)" title="View Student Record" /

to progress to another menu of that page, after multiple google searches and documentations i decided on using XPath

HtmlImage image = page.<HtmlImage>getFirstByXPath("//img[@src=\'data:image/png;base64, sc.PhotoLocation}}\']");

page = (HtmlPage) image.click();

problem is, i'm getting a NullPointerException out of this, anything i did wrong? Thanks.

Rest Of The Code:

WebClient webClient = new WebClient();
HtmlPage page = webClient.getPage("https://myWebsite");

HtmlInput username = page.getElementByName("Template1$MenuList1$TextBoxUsername");
username.setValueAttribute("myUsername");
HtmlInput password = page.getElementByName("Template1$MenuList1$TextBoxPassword");
password.setValueAttribute("myPassword");

HtmlSubmitInput loginButton = page.getElementByName("Template1$MenuList1$ButtonLogin");
page = loginButton.click();
webClient.waitForBackgroundJavaScript(2000);
if (page.getElementByName("Template1$Control0$ButtonContinue") != null) {
    HtmlSubmitInput continueButton = page.getElementByName("Template1$Control0$ButtonContinue");
    page = continueButton.click();
    webClient.waitForBackgroundJavaScript(2000);
}

Upvotes: 1

Views: 174

Answers (1)

wak786
wak786

Reputation: 1615

Hi @Orestesk Welcome to SO,

Your xpath is not correct.

src attribute of you image is dynamic. src="data:image/png;base64, {{sc.PhotoLocation}}"

Here {{sc.PhotoLocation}} would evaluate to some value. So value of src attribute keeps changing depending on the value of sc.PhotoLocation.

Use some other strategy to select your image instead of relying on src attribute.

OR try this trick.

//img[contains(@src, 'data:image/png;base64')]

Upvotes: 1

Related Questions