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

211
Views
Why would HtmlUnitDriver not Locate WebElements but FirefoxDriver able to locate the same WebElements

I have a question.

What makes FirefoxDriver be able to locate WebElements and click on them in a java code but when running the same code with HtmlUnitDriver the same WebElements are not located. Also when running the same code on HtmlUnit(applying HtmlUnit principles) WebElements are not found, in fact the code return NullPointerException. Is there a particular reason?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Without seeing your code, I might hazard a guess that it might be because you need to enable javascript.

JavaScript is disabled by default in the HtmlUnitDriver.

If you look at the source constructor at(LICENSE Apache 2.0)

https://github.com/SeleniumHQ/htmlunit-driver/blob/master/src/main/java/org/openqa/selenium/htmlunit/HtmlUnitDriver.java#L166

  /**
   * Constructs a new instance, specify JavaScript support
   * and using the {@link BrowserVersion#getDefault() default} BrowserVersion.
   *
   * @param enableJavascript whether to enable JavaScript support or not
   */
  public HtmlUnitDriver(boolean enableJavascript) {
    this(BrowserVersion.getDefault(), enableJavascript);
  }

And the other constructors at https://github.com/SeleniumHQ/htmlunit-driver/blob/master/src/main/java/org/openqa/selenium/htmlunit/HtmlUnitDriver.java#L143-L158

/**
   * Constructs a new instance with JavaScript disabled,
   * and the {@link BrowserVersion#getDefault() default} BrowserVersion.
   */
  public HtmlUnitDriver() {
    this(BrowserVersion.getDefault(), false);
  }

  /**
   * Constructs a new instance with the specified {@link BrowserVersion}.
   *
   * @param version the browser version to use
   */
  public HtmlUnitDriver(BrowserVersion version) {
    this(version, false);
  }

You see that they provide a default false if the variable isn't provided.

So to enable javascript in the HtmlUnitDriver you'll need to provide true when initializing it, that you want JavaScript components active in it.

WebDriver driver = new HtmlUnitDriver(BrowserVersion.FIREFOX_38, true);
over 4 years ago · Santiago Trujillo Report
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!