Running Selenium with Headless Chrome Webdriver Running Selenium with Headless Chrome Webdriver python python

Running Selenium with Headless Chrome Webdriver


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

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

So my thought is that running it with headless chrome would make myscript 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 flagSee crbug.com/737678


Install & run containerized Chrome:

docker pull selenium/standalone-chromedocker run --rm -d -p 4444:4444 --shm-size=2g selenium/standalone-chrome

Connect using webdriver.Remote:

driver = webdriver.Remote('http://localhost:4444/wd/hub', DesiredCapabilities.CHROME)driver.set_window_size(1280, 1024)driver.get('https://www.google.com')


If you are using Linux environment, may be you have to add --no-sandbox as well and also specific window size settings. The --no-sandbox flag is no needed on Windows if you set user container properly.

Use --disable-gpu only on Windows. Other platforms no longer require it. The --disable-gpu flag is a temporary work around for a few bugs.

//Headless chrome browser and configure            WebDriverManager.chromedriver().setup();            ChromeOptions chromeOptions = new ChromeOptions();            chromeOptions.addArguments("--no-sandbox");            chromeOptions.addArguments("--headless");            chromeOptions.addArguments("disable-gpu");//          chromeOptions.addArguments("window-size=1400,2100"); // Linux should be activate            driver = new ChromeDriver(chromeOptions);