【发布时间】: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