How to hide Chrome Driver in python?

mikezang picture mikezang · Feb 27, 2017 · Viewed 26.2k times · Source

Though I knew people asked this question but I didn't find suitable answer so I asked again. I used PhantomJS to scrape web site, but it is very slow on Mac, so I want to tru Chrome but Chrome show browser that is is bad, can I hide it? I tried code as below, that still show a small browser window..

browser = webdriver.Chrome()
browser.set_window_position(0, 0)
browser.set_window_size(0, 0)

Answer

sealabr picture sealabr · May 25, 2018

Google announced in 4/2017 you can run in headless.

https://developers.google.com/web/updates/2017/04/headless-chrome

chrome_options = Options()
# Chrome v75 and lower:
# chrome_options.add_argument("--headless") 
# Chrome v 76 and above (v76 released July 30th 2019):
chrome_options.headless = True

chrome_options.binary_location = '/Applications/Google Chrome   Canary.app/Contents/MacOS/Google Chrome Canary'  
driver = webdriver.Chrome(executable_path=os.path.abspath(“chromedriver"),   chrome_options=chrome_options)

Few things you should make sure

  • If using Mac/Linux then chrome version should be minimum 59
  • If using Windows then chrome version should be minimum 60
  • Use the latest chromedriver as well to make sure you don't have compatibility issue