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 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')
No chrome Binary at the given path - MacOS - Selenium - Python
This resolved the issue
chrome_options.binary_location = "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
Python Selenium Beta Chrome driver uses wrong binary path
In that case, you should specify where selenium has to look to find your chrome executer with binary_location inside the Options class.
Try this one out:
Assuming your chromedriver.exe and python file are on the same folder, otherwise you'll have to specify the path of the chromedriver.exe as well.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
options = Options()
options.binary_location = r'C:\Program Files\Google\Chrome Beta\Application\chrome.exe'
browser = webdriver.Chrome(options = options, service = Service("chromedriver.exe"))
browser.get(your_url)
What is the difference in chromedriver_binary and chomedriver.exe in selenium python
chromedriver-binary
chromedriver-binary downloads and installs the chromedriver binary version 97.0.4692.36 for automated testing of webapps. The installer supports Linux, MacOS and Windows operating systems.
To install:
pip install chromedriver-binary
Usage: To use chromedriver you need the following import:
import chromedriver_binary
This will add the executable to your PATH so it will be found. You can also get the absolute filename of the binary using:
chromedriver_binary.chromedriver_filename
However with Selenium v3.x you can download the ChromeDriver and use the key executable_path
to pass the absolute path of the ChromeDriver.
browser = webdriver.Chrome(executable_path=r"C:\path\to\chromedriver.exe")
Conclusion
There is no best practices defined neither any efficiency matrix comparing the two approaches. It's the user perspective of comfortness. The only bonus point using executable_path
is, you don't require to install any additional package.
How to configure ChromeDriver to initiate Chrome browser in Headless mode through Selenium?
So after correcting my code to:
options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches",["ignore-certificate-errors"])
options.add_argument('--disable-gpu')
options.add_argument('--headless')
chrome_driver_path = "C:\Python27\Scripts\chromedriver.exe"
The .exe file still came up when running the script. Although this did get rid of some extra output telling me "Failed to launch GPU process".
What ended up working is running my Python script using a .bat file
So basically,
- Save python script if a folder
Open text editor, and dump the following code (edit to your script of course)
c:\python27\python.exe c:\SampleFolder\ThisIsMyScript.py %*
Save the .txt file and change the extension to .bat
- Double click this to run the file
So this just opened the script in Command Prompt and ChromeDriver seems to be operating within this window without popping out to the front of my screen and thus solving the problem.
Python Selenium Chrome Webdriver
You need to specify the path where your chromedriver is located.
Download chromedriver for your desired platform from here.
Place chromedriver on your system path, or where your code is.
If not using a system path, link your
chromedriver.exe
(For non-Windows users, it's just calledchromedriver
):browser = webdriver.Chrome(executable_path=r"C:\path\to\chromedriver.exe")
(Set
executable_path
to the location where your chromedriver is located.)If you've placed chromedriver on your System Path, you can shortcut by just doing the following:
browser = webdriver.Chrome()
If you're running on a Unix-based operating system, you may need to update the permissions of chromedriver after downloading it in order to make it executable:
chmod +x chromedriver
That's all. If you're still experiencing issues, more info can be found on this other StackOverflow article: Can't use chrome driver for Selenium
Related Topics
Run Python Script At Startup in Ubuntu
Simulating Key Press Event Using Python For Linux
How to Clone a List So That It Doesn't Change Unexpectedly After Assignment
Get the Data Received in a Flask Request
Running Shell Command and Capturing the Output
How to Read/Process Command Line Arguments
Difference Between @Staticmethod and @Classmethod
Why Can't Python Parse This Json Data
Fatal Error: Python.H: No Such File or Directory
Converting Datetime.Date to Utc Timestamp in Python
What Do I Need to Read Microsoft Access Databases Using Python
Understanding Python Subprocess.Check_Output'S First Argument and Shell=True
Are Dictionaries Ordered in Python 3.6+
What Exactly Do "U" and "R" String Prefixes Do, and What Are Raw String Literals
How to Put a Variable'S Value Inside a String
Get Statistics For Each Group (Such as Count, Mean, etc) Using Pandas Groupby