Set chrome browser binary through chromedriver in Python Set chrome browser binary through chromedriver in Python linux linux

Set chrome browser binary through chromedriver in Python


You can set Chrome Browser Binary location through ChromeDriver using Python ing the following different ways:


Using Options

from selenium import webdriverfrom selenium.webdriver.chrome.options import Optionsoptions = 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 webdriverfrom selenium.webdriver.common.desired_capabilities import DesiredCapabilitiescap = DesiredCapabilities.CHROMEcap = {'binary_location': "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe"}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 webdriverimport selenium.webdriver.chrome.service as serviceservice = service.Service('C:\\Utility\\BrowserDrivers\\chromedriver.exe')service.start()capabilities = {'chrome.binary': "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe"}driver = webdriver.Remote(service.service_url, capabilities)driver.get('http://www.google.com')


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

And then:

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')