Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running Selenium with Headless Chrome Webdriver

So I'm trying some stuff out with selenium and I really want it to be quick.

So my thought is that running it with headless chrome would make my script faster.

First is that assumption correct, or does it not matter if i run my script with a headless driver?

Anyways I still want to get it to work to run headless, but I somehow can't, I tried different things and most suggested that it would work as said here in the October update

How to configure ChromeDriver to initiate Chrome browser in Headless mode through Selenium?

But when I try that, I get weird console output and it still doesn't seem to work.

Any tipps appreciated.

like image 570
Rhynden Avatar asked Dec 06 '18 18:12

Rhynden


People also ask

How do I run Selenium headless in Chrome?

You can run Google Chrome in headless mode simply by setting the headless property of the chromeOptions object to True. Or, you can use the add_argument() method of the chromeOptions object to add the –headless command-line argument to run Google Chrome in headless mode using the Selenium Chrome web driver.

How do I run ChromeDriver in headless mode?

ChromeOptions options = new ChromeOptions() options. addArgument("headless"); ChromeDriver driver = new ChromeDriver(options); In the above code, the browser is instructed to run in the headless mode using the addArgument() method of the ChromeOptions class provided by the Selenium WebDriver.

Does Selenium support headless browser?

Headless testing is simply running your Selenium tests using a headless browser. It operates as your typical browser would, but without a user interface, making it excellent for automated testing.


1 Answers

To run chrome-headless just add --headless via chrome_options.add_argument, i.e.:

from selenium import webdriver  from selenium.webdriver.chrome.options import Options chrome_options = Options() #chrome_options.add_argument("--disable-extensions") #chrome_options.add_argument("--disable-gpu") #chrome_options.add_argument("--no-sandbox") # linux only chrome_options.add_argument("--headless") # chrome_options.headless = True # also works driver = webdriver.Chrome(options=chrome_options) start_url = "https://duckgo.com" driver.get(start_url) print(driver.page_source.encode("utf-8")) # b'<!DOCTYPE html><html xmlns="http://www.... driver.quit() 

So my thought is that running it with headless chrome would make my script faster.

Try using chrome options like --disable-extensions or --disable-gpu and benchmark it, but I wouldn't count with much improvement.


References: headless-chrome

Note: As of today, when running chrome headless on Windows., you should include the  --disable-gpu flag See crbug.com/737678

like image 96
Pedro Lobito Avatar answered Sep 21 '22 00:09

Pedro Lobito