Unable to launch HtmlUnitdriver through Selenium 3

2020-02-16 04:58发布

I am tried to run the HtmlUnitDriver with selenium 3.4 and chrome Version 64.0.3282.119 (Official Build) (32-bit). My code is:

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());

    }
    }

2条回答
祖国的老花朵
2楼-- · 2020-02-16 05:01

To invoke HtmlUnitDriver with Selenium v3.4.0 you don't need ChromeDriver or Chrome Browser Client instead you can use the org.openqa.selenium.htmlunit.HtmlUnitDriver module following the solution below :

  • Code Block :

    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());
        }
    }
    
  • Console Output :

    Invoking Facebook through HtmlUnitDriver
    Facebook – log in or sign up
    

Note : Ensure that HtmlUnitDriver() is not resolved from :

com.gargoylesoftware.htmlunit.BrowserVersion;
查看更多
该账号已被封号
3楼-- · 2020-02-16 05:14

Use this code to Initialize headless browser :

HtmlUnitDriver driver = new HtmlUnitDriver(BrowserVersion.CHROME ,true);
driver.get("your web URL");  
查看更多
登录 后发表回答