无法通过Selenium 3.4.0启动HtmlUnitdriver

问题描述 投票:-1回答:2

我试图用selenium 3.4和chrome Version 64.0.3282.119(官方构建)(32位)运行HtmlUnitDriver。我的代码是:

package eclipse;

import java.io.File;

import org.openqa.selenium.By;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.UnexpectedAlertBehaviour;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;  
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.testng.Assert;
import org.testng.annotations.Test;

import com.gargoylesoftware.htmlunit.BrowserVersion;

public class Unit
{
    WebDriver driver;
    public static void main(String[] args){
        WebDriver driver;
        driver = new HtmlUnitDriver(BrowserVersion.CHROME);
        driver.get("http://www.google.com");
        System.out.println(driver.getTitle());

    }
    }
selenium selenium-webdriver webdriver ghostdriver htmlunit-driver
2个回答
0
投票

使用此代码初始化无头浏览器:

HtmlUnitDriver driver = new HtmlUnitDriver(BrowserVersion.CHROME ,true);
driver.get("your web URL");  

0
投票

要使用Selenium v​​3.4.0调用HtmlUnitDriver,您不需要ChromeDriver或Chrome浏览器客户端,而是可以按照以下解决方案使用org.openqa.selenium.htmlunit.HtmlUnitDriver模块:

  • 代码块: package HtmlUnitDriver; import org.openqa.selenium.WebDriver; import org.openqa.selenium.htmlunit.HtmlUnitDriver; public class htmlUnitDriver { public static void main(String[] args) { WebDriver driver = new HtmlUnitDriver(); driver.manage().window().maximize(); driver.get("https://www.facebook.com/"); System.out.println("Invoking Facebook through HtmlUnitDriver"); System.out.println(driver.getTitle()); } }
  • 控制台输出: Invoking Facebook through HtmlUnitDriver Facebook – log in or sign up

注意:确保HtmlUnitDriver()未解析:

com.gargoylesoftware.htmlunit.BrowserVersion;
© www.soinside.com 2019 - 2024. All rights reserved.