Using Selenium on Mac Chrome
Asked Answered
B

10

40

Recently got a mac and was able to run Mozilla without any issues but having trouble installing chrome extensions and running it for selenium. Can someone guide me through the process of installing the extension and running selenium on Mac chrome.

Ballonet answered 28/6, 2016 at 15:58 Comment(0)
I
18

I think that the easy way for running mac osx, chrome and selenium together is like this on mac os terminal:

# download selenium jar
curl -L0 https://selenium-release.storage.googleapis.com/3.9/selenium-server-standalone-3.9.1.jar -o selenium-server-standalone.jar

# install chromedriver using cask
brew cask install chromedriver

# start chrome driver
brew services start chromedriver                                                                                                                                                                      
#==> Successfully started `chromedriver` (label:homebrew.mxcl.chromedriver)

# start selenium server
java -jar selenium-server-standalone.jar                                                                                                                                                                           
#14:38:20.684 INFO - Selenium build info: version: '3.9.1', revision: '63f7b50'
#14:38:20.685 INFO - Launching a standalone Selenium Server on port 4444
Ichnite answered 9/3, 2018 at 13:55 Comment(3)
When running 'brew services start chromedriver', I get 'No available formula with the name "chromedriver"', any idea why?Equipotential
Maybe now has a new name, or formula, check with this brew services list, it should be installedIchnite
Nope, even after running brew cask install chromedriver theres nothing under brew services listAndrogyne
O
42

If you want to use Selenium WebDriver with Chrome, first download ChromeDriver - WebDriver for Chrome. This can be installed via Homebrew with brew install chromedriver, or manually by downloading, extracting, moving and setting the PATH as follows:

$ cd $HOME/Downloads
$ wget http://chromedriver.storage.googleapis.com/2.22/chromedriver_mac32.zip
$ unzip chromedriver_mac32.zip
$ mkdir -p $HOME/bin
$ mv chromedriver $HOME/bin
$ echo "export PATH=$PATH:$HOME/bin" >> $HOME/.bash_profile

Source: install and set path to chromedriver on mac or linux

You should then read Getting started with ChromeDriver on Desktop, in particular the sample code which shows how you map the path to the executable and instantiate ChromeDriver. If you have a reference to the driver in the PATH variable, you can omit the configuration line.

You can install both packed (.crx file) and unpacked (directory) extensions via ChromeDriver. See the code snippets for setting either up here.

If you were using Selenium IDE for FireFox instead, there is no version available for Chrome. The best alternative I know of is iMacros for Chrome.

Osteotome answered 28/6, 2016 at 22:31 Comment(3)
Thanks for answer, can you please guide me through the process of installing brew install chromedriver. I tried to install it from terminal but cant seem to do it. I have also searched for a solution online but dint work out.Ballonet
@Ballonet You need to install the Homebew package manager. You just need to run the command they put the website, which downloads the installation source and executes it via the ruby interpreterOsteotome
I have done using "brew tap homebrew/cask" and "brew cask install chromedriver" on Terminal (MacOS)Curtilage
E
31

Sometimes you will face a problem with the old version of chromedriver and when you try to install it using this command:

brew cask install chromedriver

It shows you the following:

Error: It seems there is already a Binary at '/usr/local/bin/chromedriver'; not linking.

However, you can the following step:

brew cask reinstall chromedriver

If it still shows you the same error, you can remove it with the following command

rm /usr/local/bin/chromedriver

and install it again

brew cask install chromedriver

You should have the last updated version of chrome driver

Ehrenburg answered 31/7, 2018 at 0:11 Comment(1)
Thanks buddy. Didn't hit me the idea of just removing the driver and install.Betatron
I
18

I think that the easy way for running mac osx, chrome and selenium together is like this on mac os terminal:

# download selenium jar
curl -L0 https://selenium-release.storage.googleapis.com/3.9/selenium-server-standalone-3.9.1.jar -o selenium-server-standalone.jar

# install chromedriver using cask
brew cask install chromedriver

# start chrome driver
brew services start chromedriver                                                                                                                                                                      
#==> Successfully started `chromedriver` (label:homebrew.mxcl.chromedriver)

# start selenium server
java -jar selenium-server-standalone.jar                                                                                                                                                                           
#14:38:20.684 INFO - Selenium build info: version: '3.9.1', revision: '63f7b50'
#14:38:20.685 INFO - Launching a standalone Selenium Server on port 4444
Ichnite answered 9/3, 2018 at 13:55 Comment(3)
When running 'brew services start chromedriver', I get 'No available formula with the name "chromedriver"', any idea why?Equipotential
Maybe now has a new name, or formula, check with this brew services list, it should be installedIchnite
Nope, even after running brew cask install chromedriver theres nothing under brew services listAndrogyne
B
8

I guess you have a couple of options.

  • either manually downloading chrome driver and adding it to your PATH,
  • or using webdriver_manager package

Manually downloading chromedriver

The first option is to manually download chromedriver and add it your PATH:

Step 1: Download chromedriver

# You can find more recent/old versions at http://chromedriver.storage.googleapis.com/
wget http://chromedriver.storage.googleapis.com/81.0.4044.69/chromedriver_mac64.zip

Step 2: Add chromedriver to your PATH (e.g. /usr/local/bin)

unzip chromedriver_mac64.zip
cp chromedriver /usr/local/bin

You should now be able to run

from selenium import webdriver

browser = webdriver.Chrome()
browser.get('http://localhost:8000')

without any issues


Using webdriver_manager

Alternatively, you can use webdriver_manager that contains most web drivers.

First install it with pip:

pip install webdriver_manager

And for Chrome in particular, you should do

from webdriver_manager.chrome import ChromeDriverManager


browser = webdriver.Chrome(ChromeDriverManager().install())
browser.get('http://localhost:8000')
Bowerman answered 3/5, 2020 at 18:29 Comment(0)
D
6
  1. One way is if you have homebrew on your mac, then on terminal, use this command brew install chromedriver
  2. Then you need to download chromedriver on your machine, do it from http://chromedriver.storage.googleapis.com/index.html Download latest version It will look like, "chromedriver_mac32.zip" (doesn't matter if its 32 bit, it will work for 64 bit MAC as well)
  3. Use this code for open Chrome if your chromedriver that you downloaded is inside your project folder and looks like this ..Project folder/Chrome/chromedriver

     System.setProperty("webdriver.chrome.driver", 
     System.getProperty("user.dir")+"/Chrome/chromedriver");
     driver=new ChromeDriver();
    
Dachia answered 21/10, 2017 at 9:59 Comment(1)
Thanks for the tip to just list out the actual full path to chromedriver!Sass
F
3

Use chrome driver, download from here https://sites.google.com/chromium.org/driver/

Add the file in pycharm project, then use this cmd to disable permission pop up

xattr -d com.apple.quarantine chromedriver

Also in python program, use these lines, make sure chrome is installed in Applications

import selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

options = webdriver.ChromeOptions()
options.binary_location = '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'

driver = webdriver.Chrome('/Users/<Username>/PycharmProjects/selenium_py/chromedriver')
driver.get('https://www.google.com')
Feverwort answered 3/11, 2021 at 15:55 Comment(0)
E
2

Install chromedriver directly with brew install chromedriver. The chrome driver is located in /usr/local/bin/chromedriver. If you reload your terminal with source ~/.bashrc or source ~/.zshrc, depending on your setup or restart the terminal if should work. If it does not work add the chrome driver to your PATH. You need to add /usr/local/bin/chromedriverto your ~/.bashrc or ~/.zshrc by adding the line: export PATH=$HOME/bin:/usr/local/bin:$PATH.

Endometrium answered 30/3, 2021 at 9:48 Comment(0)
S
0

The easiest way to do this after downloading the right chromedriver is with the oneliner: sudo chmod a+x chromedriver && sudo mv chromedriver /usr/local/bin/chromedriver

Sparerib answered 19/12, 2020 at 18:52 Comment(0)
H
0

MacOs Solution:

After downloading the chromedriver from https://storage.googleapis.com/chrome-for-testing-public/124.0.6367.91/mac-arm64/chromedriver-mac-arm64.zip, and installing selenium with pip install selenium, the below code should work while bypassing google security constraints.

from selenium import webdriver

# Setup Chrome options
chrome_options = webdriver.ChromeOptions()
chrome_options.binary_location = "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
profile_path = "/Users/<username>/Library/Application Support/Google/Chrome/chromeProfile"
chrome_options.add_argument(f"user-data-dir={profile_path}")
chrome_options.add_argument("disable-infobars")
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--disable-dev-shm-usage")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])

# Disable password manager
prefs = {
    "credentials_enable_service": False,
    "profile.password_manager_enabled": False,
}
chrome_options.add_experimental_option("prefs", prefs)

# Remote debugging to prevent Chrome from detecting automation
chrome_options.add_argument("--remote-debugging-port=9222")

# Create WebDriver instance
driver_path = "<driver_path>"
driver = webdriver.Chrome(driver_path, options=chrome_options)

# Now you can control the browser, navigating to Twitter
driver.get("https://www.google.com")
Handfast answered 30/4 at 14:45 Comment(0)
A
0
brew install --cask chromedriver

There is install to for example:
/opt/homebrew/Caskroom/chromedriver/126.0.6478.63/chromedriver-mac-arm64/chromedriver

cp /opt/homebrew/Caskroom/chromedriver/126.0.6478.63/chromedriver-mac-arm64/chromedriver /usr/local/bin

If you got error with permissions run command bellow:

sudo xattr -d com.apple.quarantine path/to/chromedrive
Agulhas answered 22/6 at 13:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.