Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

209
Views
How to click a HtmlButton pop-up on the HtmlUnit test

I have the following HtmlUnit test on the attached code.

How do I click a pop-up CONFIRM AGE BUTTON

htmlButton3 = (HtmlButton)
getSpribePage.getFirstByXPath("//div[@class='modal-content']//button[1]");

The end result of the test is to get the value counting.

When going to a website manually here, you click the "DEMO" button, "CONFIRM AGE" button and you will be taken to the game.

I'm more interested in using an HtmlUnit not HtmlUnitDriver.

import java.io.IOException; 
import java.net.MalformedURLException;

import com.gargoylesoftware.htmlunit.BrowserVersion;

import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;

import com.gargoylesoftware.htmlunit.WebClient;

import com.gargoylesoftware.htmlunit.WebClientOptions;

import com.gargoylesoftware.htmlunit.html.HtmlButton;

import com.gargoylesoftware.htmlunit.html.HtmlPage;

import com.gargoylesoftware.htmlunit.html.HtmlTextInput;

public class AviatorHtmlUnit { 

public static void main(String[] args) throws Exception { 

WebClient webclient = new WebClient(BrowserVersion.CHROME);

HtmlButton htmlButton1; 
HtmlButton htmlButton2; 
HtmlButton htmlButton3;

webclient.getOptions().setThrowExceptionOnScriptError(false);

webclient.getOptions().setJavaScriptEnabled(true);

java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(java.util.lo gging.Level.OFF);

//webclient.getOptions().setCssEnabled(false);
 webclient.getOptions().setThrowExceptionOnScriptError(false);
 
webclient.getOptions().setUseInsecureSSL(true);
 webclient.getCookieManager().setCookiesEnabled(true);
 
String SprPage = "https://www.spribe.co/games/aviator"; 

HtmlPage getSpribePage = webclient.getPage(SprPage);

//CLICK COOKIE POP-UP BUTTON
//htmlButton1 = (HtmlButton)
getSpribePage.getFirstByXPath("//button[text()=' Got it']"); 

//PLAY DEMO
htmlButton2 = (HtmlButton)
getSpribePage.getFirstByXPath("//button[text()='Play Demo ']"); 

//CLICK CONFIRM AGE BUTTON 
htmlButton3 = (HtmlButton)
getSpribePage.getFirstByXPath("//div[@class='modal-content']//button[1]"); 

//GET NEWLY OPENED  WINDOW
List<WebWindow> windows = webclient.getWebWindows();

webclient.setCurrentWindow(windows.get(1)); 

//GET ROUND COUNTER VALUE 
roundCounter = (HtmlTextInput)
getSpribePage.getFirstByXPath("//div[contains(@class,'coefficient')]");
System.out.println(roundCounter.asText());

webclient.close(); 

     } 
 }

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!