每当我尝试将 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);
此错误消息...
org.openqa.selenium.WebDriverException: com.gargoylesoftware.htmlunit.ScriptException: Exception invoking getOffsetTop
。意味着您的程序遇到脚本异常异常。
有关您正在使用的二进制文件版本的更多信息将帮助我们以更好的方式调试根本原因:
- 硒
- HtmlUnitDriver
但是,您可以使用下面提到的配置和解决方案:
配置:
- 硒: 3.14.0
- HtmlUnitDriver: 2.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