【问题标题】:Script not working in HtmlUnitDriver脚本在 HtmlUnitDriver 中不起作用
【发布时间】:2016-04-23 06:03:36
【问题描述】:

我的目的是为测试自动化执行无头浏览。我在 Java 中使用 selenium webdriver。

现在,问题是脚本在 Firefox 浏览器中运行良好,但在 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情况下的错误:

    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

【问题讨论】:

  • 您确定给它足够的时间来加载TimeoutException: Timed out after 10 seconds。无头浏览器有一些棘手的情况,但 id 选择器不是其中之一。你也可以试试ghostdriver

标签: java selenium headless-browser htmlunit-driver


【解决方案1】:

它不起作用,因为默认情况下 HtmlUnitDriver 禁用了 javascript。查看默认构造函数代码:

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

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

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

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

【讨论】:

  • 感谢您编辑的答案。我可以通过合并正确的依赖来解决 NoClassDefFoundError。
  • 西里尔,请看一下这个问题:stackoverflow.com/questions/34891035/…
  • 你能指定正确的依赖吗? TH 的回答和 cmets 不知道它们是什么!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-27
相关资源
最近更新 更多