【问题标题】:Replacing firefox driver with htmlunit driver用htmlunit驱动替换firefox驱动
【发布时间】:2013-06-13 23:58:12
【问题描述】:

您好,请问如何使用 HTMLUNIT DRIVER 代替 FIREFOX 驱动程序运行此示例程序。 下面的代码在firefox驱动下运行成功,但在htmlunit驱动下运行失败

org.openqa.selenium.NoSuchElementException:无法使用.//*[contains(concat(' ',normalize-space(@class),' '),' gssb_e ')]-EXCEPTION 定位节点。

import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;

public class GoogleSuggest
{
    public static void main(String[] args) throws Exception
    {
        WebDriver driver = new FirefoxDriver();
        driver.get("http://www.google.com/webhp?complete=1&hl=en");

        WebElement query = driver.findElement(By.name("q"));
        query.sendKeys("Cheese");

        long end = System.currentTimeMillis() + 50000;
        while (System.currentTimeMillis() < end)
        {
            WebElement resultsDiv = driver.findElement(By.className("gssb_e"));
            if (resultsDiv.isDisplayed())
            {
                break;
            }
        }

        List<WebElement> allSuggestions =
        driver.findElements(By.xpath("//td[@class='gssb_a gbqfsf']"));
        for (WebElement suggestion : allSuggestions)
        {
            System.out.println(suggestion.getText());
        }
    }
}

请任何人告诉我如何使用 HTMLUNIT 驱动程序 n IMA 非常刚开始并解释我的原因,如果有人发布使用 HTMLUNIT 驱动程序操作的相同代码,我会很高兴,还请告诉我如何克服使用 HTMLUNIT 驱动程序时的 DEFAULTCSSERROR 再次不是 firefox 驱动程序的问题。

我的主要目的是在后台运行上述进程,而不调用浏览器使所有内容不可见。

任何人请在这方面帮助我。

【问题讨论】:

  • 您的错误消息甚至与您正在运行的代码不匹配。 在您的代码中根本没有在哪里您在运行.//*[contains(concat(' ',normalize-space(@class),' '),' gssb_e ')] ...所以请将您的实际代码放在问题中并扩展什么是你说的这个“DEFAULTCSSERROR”?什么时候发生的?什么线路?
  • 安排我发布的代码是正确的,当我在上面的代码中用 htmlunitdriver 替换 firefoxdriver 时,我得到了同样的错误。好的,让我准确地解释我的需要 - 我得到执行上面的输出netbeans 中的代码是芝士蛋糕工厂芝士蛋糕工厂菜单、芝士蛋糕食谱、芝士、芝士蛋糕、芝士蛋糕工厂营养等等,........,我的愿望是我想要使用 HTMLUNIT DRIVER REPLACING WITH FIREFOX 获得相同的输出驱动程序。如果您可以使用 HtmlUnit 驱动程序发布相同的代码,以获得相同的输出。请帮助它对我来说非常重要。

标签: css webdriver selenium-webdriver htmlunit selenium-firefoxdriver


【解决方案1】:

HtmlUnit 驱动程序 FirefoxDriver

"If you test javascript using HtmlUnit the results 
      may differ significantly from those browsers"

看看here

【讨论】:

  • e1che 我尝试使用 HtmlUnitDriver 启用 javascript 但我无法获得与我在上述程序中使用 firefox 驱动程序获得相同的输出。如果您可以使用 HtmlUnit 驱动程序发布相同的代码作为相同的输出已获得。PLZ HELP。
【解决方案2】:

在 HtmlUnit Driver 中,它只会寻找小写的标签和属性。

示例: html

输入类型=“文本”名称=“示例”>

INPUT type="text" name="other" >

//网络驱动代码

driver.findElements(By.xpath("//input"));

对于 HtlmUNIt 案例:它只会找到一个元素(name="example")

对于 firefoxDriver 案例 = 它会找到 2 个元素

希望它会帮助你调试代码

【讨论】:

    猜你喜欢
    • 2023-04-09
    • 2017-09-11
    • 1970-01-01
    • 2013-01-29
    • 1970-01-01
    • 2018-02-11
    • 2020-06-16
    • 1970-01-01
    • 2015-09-21
    相关资源
    最近更新 更多