Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set chrome browser binary through chromedriver in Python

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?

like image 409
user123 Avatar asked Aug 04 '17 07:08

user123


People also ask

How do I change the binary path in Chrome?

ChromeOptions optionsBeta = new ChromeOptions(); optionsBeta. setBinary(“path\\to\\chrome\\browser\\beta\\binary”); WebDriver driver = new ChromeDriver(optionsBeta);

How do I change ChromeDriver path?

Go to the terminal and type the command: sudo nano /etc/paths. Enter the password. At the bottom of the file, add the path of your ChromeDriver. Type Y to save.


2 Answers

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')
like image 174
undetected Selenium Avatar answered Sep 28 '22 17:09

undetected Selenium


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", )
like image 33
Rembau Avatar answered Sep 28 '22 18:09

Rembau