How to use HtmlUnitDriver using Selenium in python?

574 Views Asked by At

I'm using selenium-4.1.0 and I'm searching for the lightest webdriver possible (for speed purpose).
I heard about HtmlUnitDriver, but when using python, I need to run a selenium server before trying to use the driver. I was careful to take the version corresponding to my selenium version (available here), and I followed exactly the steps to make it work; but it didn't.

After running java -jar selenium-server-4.1.0.jar standalone (running on localhost:4444):

I tried:

from selenium import webdriver

driver = webdriver.Remote(
    command_executor='http://localhost:4444/wd/hub', # I also tried with 'http://localhost:4444', and without 'command_executor' keyword
    desired_capabilities=webdriver.DesiredCapabilities.HTMLUNITWITHJS
)
driver.get('http://www.google.com')

And I got the following error:

DeprecationWarning: desired_capabilities has been deprecated, please pass in an Options object with options kwarg
  driver = webdriver.Remote(

So I changed my code to:

from selenium import webdriver

options = webdriver.ChromeOptions()
options.headless = True

wd = webdriver.Remote('http://localhost:4444/wd/hub', options=options)

wd.get('http://www.google.com')

wd.save_screenshot('screenshot.png')

And it worked (I got the screenshot), but then I was no longer using HtmlUnit.
Does anyone know what's wrong and/or what I could do to make it work ?

1

There are 1 best solutions below

0
Gopi On

In the python library the selenium is best to take the screenshot. In this below code the I am keeping URL scrolling also if you need you can do neither leave it.

In this you want to check the chrome version and install the chrome drivers. My chrome version is 96.0.4664.110 download that drivers and install

import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = webdriver.ChromeOptions()
options.headless = True
driver = webdriver.Chrome(options=options)

URL = "D:\watermark"
driver.get(URL)

S = lambda X: driver.execute_script('return 
document.body.parentNode.scroll'+X)
driver.set_window_size(S('Width'),S('Height')) # May need manual adjustment
#time.sleep(60)                                                                                                                
driver.find_element_by_tag_name('body').screenshot('web_screenshot.png')
driver.quit()