【问题标题】:org.openqa.selenium.WebDriverException: com.gargoylesoftware.htmlunit.ScriptException: Exception invoking getOffsetTop with HtmlUnitDriver & Seleniumorg.openqa.selenium.WebDriverException: com.gargoylesoftware.htmlunit.ScriptException: 使用 HtmlUnitDriver & Selenium 调用 getOffsetTop 异常
【发布时间】:2019-02-08 13:26:42
【问题描述】:

每当我尝试将 HtmlUnit enableJavaScript 设置为 true 时,它​​都会返回此错误:

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

我用这个设置为真:

driver = new HtmlUnitDriver(true);

在设置 enableJavaScript(true) 之前,我收到与此特定代码行有关的错误:

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

上一条错误信息:

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

所以基本上,在将其设置为 true 后,我收到另一个全新的错误。 更新 我通过添加这个来解决这个问题:

public class CustomHtmlUnitDriver extends HtmlUnitDriver {


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

然后我添加了((CustomHtmlUnitDriver) driver).setJavascriptEnabled(true);

【问题讨论】:

    标签: java selenium selenium-webdriver webdriver htmlunit-driver


    【解决方案1】:

    此错误消息...

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

    ...暗示你的程序遇到了ScriptException异常。

    有关您正在使用的二进制文件版本的更多信息将有助于我们以更好的方式调试根本原因:

    • HtmlUnitDriver

    但是,您可以使用下面提到的配置和解决方案:

    • 配置:

      • 3.14.0
      • HtmlUnitDriver2.33.0
    • 代码块:

      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();
          }
      }
      
    • 控制台输出:

      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
      

    【讨论】:

    • 尝试设置 enableJavaScript(true) 时仍然收到“getOffsetTop”
    • HtmlUnitDriver: 2.33.0 默认配置enableJavaScript(true)。对默认配置的调整以不同的方式完成。您可以复制/粘贴并测试代码并相应地更新我吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-31
    • 1970-01-01
    相关资源
    最近更新 更多