-1

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

    }
    }
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Naveen
  • 1
  • 1

2 Answers2

0

Use this code to Initialize headless browser :

HtmlUnitDriver driver = new HtmlUnitDriver(BrowserVersion.CHROME ,true);
driver.get("your web URL");  
cruisepandey
  • 28,520
  • 6
  • 20
  • 38
0

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;
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I am tried with this : But output is - log4j:WARN No appenders could be found for logger (com.gargoylesoftware.htmlunit.WebClient). log4j:WARN Please initialize the log4j system properly. Invoking Facebook through HtmlUnitDriver – Naveen May 14 '18 at 12:25
  • can you please tell me the HtmlUnitDriver compatible version ? – Naveen May 14 '18 at 12:52
  • @Naveen This code block will work on **Selenium v3.4.0** and the recent **Selenium v3.12.0**. Simply copy-paste the code and change the _Package Name_ – undetected Selenium May 14 '18 at 12:55
  • I am using v3.4.0... i have tried simply copy and paste the code but page title are not coming for me ? – Naveen May 14 '18 at 12:57
  • @Naveen These warning messages **log4j:WARN No appenders could be found for logger (com.gargoylesoftware.htmlunit.WebClient). log4j:WARN Please initialize the log4j system properly.** are coming as you haven't configured `Log4j` module for this project. Else you are getting proper _Console Output_ as follows **Invoking Facebook through HtmlUnitDriver** I think your program is executing just perfect :) – undetected Selenium May 14 '18 at 13:01
  • Version for Log4j ? – Naveen May 14 '18 at 13:06
  • @Naveen Leave out `Log4j` for now. That is a separate module to generate more verbose _Log Messages_. Your program is working fine. – undetected Selenium May 14 '18 at 13:07
  • If reinstalled the log4j its enough ? otherwise how I will get output ? – Naveen May 14 '18 at 13:10
  • @Naveen Cool down man, take a deep breathe, your program is working fine. You are getting expected output in the console as **Invoking Facebook through HtmlUnitDriver** – undetected Selenium May 14 '18 at 13:14
  • The other _log messages_ are `WARN` messages and you can ignore them for the time being. – undetected Selenium May 14 '18 at 13:15