Running Selenium with Headless Chrome Webdriver

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 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

Webdriver Manager+Chrome Headless+Selenium+Python: webdriver does not respond to options

--headless should come with --window-size

Ex: "--window-size=1920,1080"

Detected on Headless Chrome Selenium

To avoid detection using google-chrome-headless add the following argument through add_argument() as follows:

--disable-blink-features=AutomationControlled

Sample Code:

options = Options()
options.headless = True
options.add_argument("start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('excludeSwitches', ['enable-logging'])
options.add_experimental_option('useAutomationExtension', False)
options.add_argument('--disable-blink-features=AutomationControlled')
s = Service('C:\\BrowserDrivers\\chromedriver.exe')
driver = webdriver.Chrome(service=s, options=options)
driver.get("https://www.amazon.com.au/Oculus-Quest-2-Virtual-Reality-Headset/dp/B08FSZ8QWH")

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

So after correcting my code to:

options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches",["ignore-certificate-errors"])
options.add_argument('--disable-gpu')
options.add_argument('--headless')
chrome_driver_path = "C:\Python27\Scripts\chromedriver.exe"

The .exe file still came up when running the script. Although this did get rid of some extra output telling me "Failed to launch GPU process".

What ended up working is running my Python script using a .bat file

So basically,

  1. Save python script if a folder
  2. Open text editor, and dump the following code (edit to your script of course)

    c:\python27\python.exe c:\SampleFolder\ThisIsMyScript.py %*

  3. Save the .txt file and change the extension to .bat

  4. Double click this to run the file

So this just opened the script in Command Prompt and ChromeDriver seems to be operating within this window without popping out to the front of my screen and thus solving the problem.

Can i turn off headless chrome while code is running then turn it on back again?

No, you can't.

webdriver object is created with specified (or default) parameters that can't be changed during the session.



Related Topics



Leave a reply



Submit