I’m attempting to configure Chrome’s headless mode on my Mac, but I keep encountering errors. I’ve referred to various tutorials and discussions online regarding the setup. Previously, I had success using PhantomJS for a headless browser, although I’ve learned that Selenium recommends not using it anymore. Here is the script I’ve been working with, based on suggestions from online resources:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument('--headless')
service = Service('path_to_chromedriver')
driver = webdriver.Chrome(service=service, options=options)
driver.get('http://www.example.com')
print('Headless Chrome has been launched')
However, I receive this error message:
Traceback (most recent call last):
File "headless_chrome.py", line 20, in <module>
driver = webdriver.Chrome(service=service, options=options)
selenium.common.exceptions.WebDriverException: Message: unknown error: can’t locate Chrome binary
Can anyone provide insight into resolving this issue?
To resolve the issue of Chrome's binary not being located, follow these steps to efficiently configure Chrome's headless mode on your Mac:
- Ensure Chrome is installed on your Mac. Verify its location typically at
/Applications/Google Chrome.app/Contents/MacOS/Google Chrome
.
- Modify your script to explicitly set the Chrome binary location:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument('--headless')
options.binary_location = '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'
service = Service('path_to_chromedriver')
driver = webdriver.Chrome(service=service, options=options)
driver.get('http://www.example.com')
print('Headless Chrome has been launched')
- Ensure your
path_to_chromedriver
is correct. It should point to the executable chromedriver
you've downloaded.
- Verify compatibility between the ChromeDriver version and your Chrome browser version. You can check the version details via the ChromeDriver download page and update if necessary.
By specifying the binary location and verifying your ChromeDriver path and version, you should be able to resolve the errors and run Chrome in headless mode efficiently.