脚本在HtmlUnitDriver中不起作用

脚本在HtmlUnitDriver中不起作用

本文介绍了脚本在HtmlUnitDriver中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目的是执行无头浏览以实现测试自动化.我正在使用Java的Selenium Webdriver.

My purpose to execute headless browsing for test-automation. I am using selenium webdriver with Java.

现在,问题是脚本可以在Firefox浏览器中正常运行,但不能在HtmlUnitDriver中运行.

Now, issue is script is working fine in Firefox browser,but not in HtmlUnitDriver.

请指导我哪里做错了.

public class Headless
{
    public static void main(String[] args) throws InterruptedException
    {
        WebDriver driver = new HtmlUnitDriver();
        //WebDriver driver=new FirefoxDriver();

// Navigate to Google
        driver.get("https://www.google.co.in/?gfe_rd=cr&ei=k36cVsa6OubI8Aec14bICQ&gws_rd=ssl");
        //Thread.sleep(14000);


        WebDriverWait wait=new WebDriverWait(driver,10);
        wait.until(ExpectedConditions.presenceOfElementLocated(By.id("sb_ifc0")));



        System.out.println("URL= "+driver.getCurrentUrl());
        // Locate the searchbox using its name
        WebElement element = driver.findElement(By.id("sb_ifc0"));

       // Enter a search query
       element.sendKeys("Guru99");

       // Submit the query. Webdriver searches for the form using the text input element automatically
       // No need to locate/find the submit button
       element.submit();

       // This code will print the page title
       System.out.println("Page title is: " + driver.getTitle());

关于HtmlUnitDriver的错误:

The error in case of HtmlUnitDriver:

    Exception in thread "main" java.lang.NoClassDefFoundError: com/gargoylesoftware/htmlunit/javascript/host/Event
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.resetKeyboardAndMouseState(HtmlUnitDriver.java:513)
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.get(HtmlUnitDriver.java:509)
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.get(HtmlUnitDriver.java:469)
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.<init>(HtmlUnitDriver.java:185)
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.<init>(HtmlUnitDriver.java:195)
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.<init>(HtmlUnitDriver.java:191)
    at com.se.ecoreal.selenium.Headless.main(Headless.java:15)
Caused by: java.lang.ClassNotFoundException: com.gargoylesoftware.htmlunit.javascript.host.Event
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 7 more

推荐答案

它不起作用,因为HtmlUnitDriver默认禁用了javascript.检查默认的构造函数代码:

It doesn't work because javascript is disabled by default for HtmlUnitDriver. Check the default constructor code:

  public HtmlUnitDriver() {
    this(false);
  }
  public HtmlUnitDriver(boolean enableJavascript) {
    this(BrowserVersion.getDefault(), enableJavascript);
  }

如果启用javascript,您的示例对我有用:

Your example works for me if I enable javascript:

 WebDriver driver = new HtmlUnitDriver(BrowserVersion.FIREFOX_38, true);

编辑(已编辑问题的答案):
NoClassDefFoundError是由缺少(或版本错误)依赖项引起的.如果使用的是maven或gradle,请检查项目是否存在依赖项冲突.如果不使用依赖项管理,请确保已包含所有HtmlUnit依赖项.

Edit (answer to the edited question):
NoClassDefFoundError is caused by a missing (or wrong version) dependency. If you are using maven or gradle, check your project for dependency conflicts. If you don't use dependency management, make sure you included all HtmlUnit dependencies.

这篇关于脚本在HtmlUnitDriver中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 07:28