How to use headless Chrome browser with selenium(python)

Using Chrome Headless

The first thing you need to do to switch to Chrome is to install Chrome if you don’t have it yet (obviously!), and then you need to install ChromeDriver, which is the little bit of glue that allows Selenium to send commands to Chrome and automate it. If you are on a Mac, then brew install chromedriver is all you need to do. On other platforms, download an installer from the ChromeDriver site: https://sites.google.com/a/chromium.org/chromedriver/downloads.

To switch to Chrome, you just need to change the initialization of the client variable:

try:
client = webdriver.Chrome()
except:
pass

But this will still use a regular Chrome window. If you want to use the headless option, you have to add options:

options = webdriver.ChromeOptions()
options.add_argument('headless')
try:
client = webdriver.Chrome(chrome_options=options)
except:
pass

If you want to switch back to the regular mode, just comment out the options.add_argument('headless') line and you’ll get a visible window that you can watch while the tests run.

Advertisements
Post a comment or leave a trackback: Trackback URL.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: