Set chrome browser binary through chromedriver in Python
Asked Answered
L

4

23

I used Selenium with Python Chrome webdriver. In my code I used:

driver = webdriver.Chrome(executable_path = PATH_TO_WEBDRIVER)

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");

Anyone has an example for Python?

Lye answered 4/8, 2017 at 7:24 Comment(0)
E
44

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 = {'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 webdriver
import selenium.webdriver.chrome.service as service
service = 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')
Espousal answered 4/8, 2017 at 10:4 Comment(7)
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.Lye
@Lye Get ChromeDriver from the right place https://sites.google.com/a/chromium.org/chromedriver ThanksEspousal
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.Lye
I only see chromedriver downloads not the actual chrome browserLye
@Lye 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 :)Espousal
chrome_options is being deprecated, instead of chrome_options=options use options=optionsHuppert
What about selenium.webdriver.ChromeOptions?Carve
A
1

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", )
Auxochrome answered 14/12, 2018 at 13:26 Comment(0)
A
1

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')
Axiom answered 20/5, 2019 at 18:31 Comment(0)
W
-2

First of all if you want to use chrome then you need to download it's binary from below URL :-

https://sites.google.com/a/chromium.org/chromedriver/

Now you need to pass this driver path to the selenium webdriver.

If you are using python the code should be like below :-

    driver = webdriver.Chrome('C:\Users\name\Downloads\chromedriver_win32 (3)\chromedriver.exe')
    driver.implicitly_wait(30) # seconds
    driver.get('https://www.google.co.in/')

Hope it will help you :)

Winn answered 4/8, 2017 at 7:36 Comment(1)
Actually, it's not what I'm looking for. Chromedriver binaries and the Chrome browser binaries are two different things. For Chromedriver I already have a working code. I'm referring to Chrome browser binaries.Lye

© 2022 - 2024 — McMap. All rights reserved.