0

Whenever I try to set HtmlUnit enableJavaScript to true, it returns this error:

org.openqa.selenium.WebDriverException: com.gargoylesoftware.htmlunit.ScriptException: Exception invoking getOffsetTop

I set it to true with this:

driver = new HtmlUnitDriver(true);

Before setting enableJavaScript(true), I was receiving an error with this specific line of code:

WebElement checkoutEmail = driver.findElement(By.id("checkout_email"));
javascriptExecutor.executeScript("arguments[0].value='abc@gmail.com';", checkoutEmail);

Previous error message:

java.lang.UnsupportedOperationException: Javascript is not enabled for this HtmlUnitDriver instance

So basically, after setting it to true, I receive another brand new error. Update I fixed this by adding this:

public class CustomHtmlUnitDriver extends HtmlUnitDriver {


    @Override
    protected WebClient modifyWebClient(WebClient client) {
        WebClient modifiedClient = super.modifyWebClient(client);
        modifiedClient.getOptions().setThrowExceptionOnScriptError(false);}}

And then I added ((CustomHtmlUnitDriver) driver).setJavascriptEnabled(true);

Jeff
  • 170
  • 2
  • 5
  • 19

1 Answers1

0

This error message...

org.openqa.selenium.WebDriverException: com.gargoylesoftware.htmlunit.ScriptException: Exception invoking getOffsetTop

...implies that your program encountered ScriptException exception.

Some more information about the version of the binaries you are using would have helped us to debug the root cause in a better way in terms of:

  • Selenium
  • HtmlUnitDriver

However, you can use the below mentioned configuration and solution:

  • Configuration:

    • Selenium: 3.14.0
    • HtmlUnitDriver: 2.33.0
  • Code Block:

    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.htmlunit.HtmlUnitDriver;
    
    
    public class A_HtmlunitDriver_2_33_0 {
    
        public static void main(String[] args) throws InterruptedException {
    
        WebDriver driver = new HtmlUnitDriver();
        driver.manage().window().maximize();
        driver.get("https://stackoverflow.com/questions/53812207/how-to-resolve-htmlunit-wrapsdriver-error");
        System.out.println("HtmlUnitDriver invoked");
        driver.quit();
        }
    }
    
  • Console Output:

    HtmlUnitDriver invoked
    19:10:32.092 [main] DEBUG com.gargoylesoftware.htmlunit.WebWindowImpl - destroyChildren
    19:10:32.093 [main] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager - Connection manager is shutting down
    19:10:32.094 [main] DEBUG org.apache.http.impl.conn.DefaultManagedHttpClientConnection - http-outgoing-1: Close connection
    19:10:32.095 [main] DEBUG org.apache.http.impl.conn.DefaultManagedHttpClientConnection - http-outgoing-0: Close connection
    19:10:32.096 [main] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager - Connection manager shut down
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Still receiving "getOffsetTop" when trying setting enableJavaScript(true) – Jeff Feb 09 '19 at 02:16
  • In _HtmlUnitDriver: 2.33.0_ `enableJavaScript(true)` is configured by default. The tweaks to default configuration are done in a different way. Can you just copy/paste and test the code and update me accordingly please? – undetected Selenium Feb 09 '19 at 13:43