yadav
yadav

Reputation: 99

chromedriver in headless mode

I am using chromedriver for web scraping by giving the binary path.

driver = webdriver.Chrome(r"C:\Program Files\JetBrains\PyCharm Community Edition 2017.3.3\bin\chromedriver.exe")
driver.get("https://www.example.com/")

This invoke chromedriver in GUI mode. How can I start chrome in headless mode?

Upvotes: 0

Views: 853

Answers (1)

telex-wap
telex-wap

Reputation: 852

Chrome headless is ideally much better than PhantomJS, whose owner decided to stop maintaining the project because the arrival of Chrome headless made it a bit less necessary. That being said, if you have a Chrome version which supports headless, you can do this:

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument('headless')
driver = webdriver.Chrome(chrome_options=options)

As you see, headless is an argument, so if for some reason you want to run the same code but you need to see the GUI, remove that argument.

By the way, if you ever wanted to give the binary location, a nice way to do it is also with options:

options.binary_location = 'path to your chrome binary'

But if your installed version is recent enough, there should be no reason to do so.

Upvotes: 1

Related Questions