Set chrome browser binary through chromedriver in Python
to point the webdriver to the webdriver executable. Is there a way to point webdriver to the Chrome Browser binaries? In https://sites.google.com/a/chromium.org/chromedriver/capabilities they have the following (which I assume it what I’m looking for):
ChromeOptions options = new ChromeOptions(); options.setBinary("/path/to/other/chrome/binary");
4 Answers 4
You can set Chrome Browser Binary location through ChromeDriver using Python ing the following different ways:
Using Options
from selenium import webdriver from selenium.webdriver.chrome.options import Options options = Options() options.binary_location = "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe" driver = webdriver.Chrome(chrome_options=options, executable_path="C:/Utility/BrowserDrivers/chromedriver.exe", ) driver.get('http://google.com/')
Using DesiredCapabilities
from selenium import webdriver from selenium.webdriver.common.desired_capabilities import DesiredCapabilities cap = DesiredCapabilities.CHROME cap = driver = webdriver.Chrome(desired_capabilities=cap, executable_path="C:\\Utility\\BrowserDrivers\\chromedriver.exe") driver.get('http://google.com/')
Using Chrome as a Service
from selenium import webdriver import selenium.webdriver.chrome.service as service service = service.Service('C:\\Utility\\BrowserDrivers\\chromedriver.exe') service.start() capabilities = driver = webdriver.Remote(service.service_url, capabilities) driver.get('http://www.google.com')
Looks like in the right direction. I’m working on a mac.I tried downloading chrome browser binaries but the only thing I found was Chromium binaries and if I try using it the driver will error-out «No chrome binaries found». On the other hand, I tried moving chrome from: /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome and use it locally but then the driver just hangs.
@user123 Get ChromeDriver from the right place https://sites.google.com/a/chromium.org/chromedriver Thanks
I saw the binary_location in the ChromeOptions documentation so I accepted the answer. Now I’m still stuck with finding the right chrome executable for Mac.
@user123 If I am not wrong you want to work with multiple versions of Chrome browser version right? Then you have to download from Chromium site only I suppose. I do the same thing with Mozilla Firefox 🙂
Thanks a lot I was struggling with this for 2.5 hours as I did not know how to set the Chrome Executable path in Python. Works now
options = Options() options.binary_location = "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe" driver = webdriver.Chrome(chrome_options=options, executable_path="C:/Utility/BrowserDrivers/chromedriver.exe", )
Is there a way to point webdriver to the Chrome Browser binaries?
As others have already stated, use binary_location . However, the location of Chrome moves around depending on the the platform. Fedora and Ubuntu use different locations. So you may want to use something like:
def get_chrome(): if os.path.isfile('/usr/bin/chromium-browser'): return '/usr/bin/chromium-browser' elif os.path.isfile('/usr/bin/chromium'): return '/usr/bin/chromium' elif os.path.isfile('/usr/bin/chrome'): return '/usr/bin/chrome' elif os.path.isfile('/usr/bin/google-chrome'): return '/usr/bin/google-chrome' else: return None
if version.parse(selenium.__version__) >= version.parse("3.0"): opts = Options() opts.binary_location = get_chrome() opts.add_argument('--headless') opts.add_argument('--no-sandbox') opts.add_argument('--disable-dev-shm-usage') driver = webdriver.Chrome(chrome_options=opts) driver.maximize_window() else: opts = Options() opts.headless = True opts.binary_location = get_chrome() driver = webdriver.Chrome(chrome_options=opts) driver.maximize_window() agent = driver.execute_script('return navigator.userAgent')
Using Extensions with Selenium (Python)
I am currently using Selenium to run instances of Chrome to test web pages. Each time my script runs, a clean instance of Chrome starts up (clean of extensions, bookmarks, browsing history, etc). I was wondering if it’s possible to run my script with Chrome extensions. I’ve tried searching for a Python example, but nothing came up when I googled this.
7 Answers 7
You should use Chrome WebDriver options to set a list of extensions to load. Here’s an example:
import os from selenium import webdriver from selenium.webdriver.chrome.options import Options executable_path = "path_to_webdriver" os.environ["webdriver.chrome.driver"] = executable_path chrome_options = Options() chrome_options.add_extension('path_to_extension') driver = webdriver.Chrome(executable_path=executable_path, chrome_options=chrome_options) driver.get("http://stackoverflow.com") driver.quit()
Hi, I tested your code. But things may seem deprecated. I am getting a traceback about the directory of the extension IsADirectoryError [Error 21] . I just want to know how to write the full path to the extension. What I have here is path/to/XXXXXXXXXXXXXXXXX/1.2.345 should I put this version number in the path??
I hope you help. I have used this answer to get the path to the extension so do you have an Idea why I am facing this error? IsADirectoryError [Error 21] . Thanks in advance.
The leading answer didn’t work for me because I didn’t realize you had to point the webdriver options toward a .zip file.
I.e. chrome_options.add_extension(‘path_to_extension_dir’) doesn’t work.
You need: chrome_options.add_extension(‘path_to_extension_dir.zip’)
After figuring that out and reading a couple posts on how to create the zip file via the command line and load it into selenium , the only way it worked for me was to zip my extension files within the same python script. This actually turned out to be a nice way for automatically updating any changes you might have made to your extension:
import os, zipfile from selenium import webdriver # Configure filepaths chrome_exe = "path/to/chromedriver.exe" ext_dir = 'extension' ext_file = 'extension.zip' # Create zipped extension ## Read in your extension files file_names = os.listdir(ext_dir) file_dict = <> for fn in file_names: with open(os.path.join(ext_dir, fn), 'r') as infile: file_dict[fn] = infile.read() ## Save files to zipped archive with zipfile.ZipFile(ext_file), 'w') as zf: for fn, content in file_dict.iteritems(): zf.writestr(fn, content) # Add extension chrome_options = webdriver.ChromeOptions() chrome_options.add_extension(ext_file) # Start driver driver = webdriver.Chrome(executable_path=chrome_exe, chrome_options=chrome_options) driver.get("http://stackoverflow.com") driver.quit()