gpt4 book ai didi

css - 用 htmlunit 驱动替换 firefox 驱动

转载 作者:行者123 更新时间:2023-11-28 18:19:57 25 4
gpt4 key购买 nike

您好,请问如何使用 HTMLUNIT 驱动程序而不是 FIREFOX 驱动程序运行此示例程序。下面的代码使用 firefox 驱动程序成功运行,但没有使用 htmlunit 驱动程序成功运行

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

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 我只是一个初学者并向我解释原因,如果有人发布使用 HTMLUNIT 驱动程序操作的相同代码我会很高兴,也请告诉我如何克服使用 HTMLUNIT 驱动程序时的 DEFAULTCSSERROR 再次不是 firefox 驱动程序的问题。

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

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

最佳答案

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

例子:HTML

input type="text" name="example" >

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

//网络驱动程序代码

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

对于 HtlmUNit 情况:它只会找到一个元素(name="example")

对于 firefoxDriver case = 它将找到 2 个元素

希望它能帮助你调试代码

关于css - 用 htmlunit 驱动替换 firefox 驱动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17157546/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com