1. 用homebrew安装较新版本的python2.7
brew install python sudo -H easy_install pip
2. 安装selenium
sudo -H pip install selenium
3. 安装selenium webdriver
Firefox:
到https://github.com/mozilla/geckodriver/releases下载macOS版本的driver解压出来,复制到/usr/local/bin
sudo cp ~/Download/geckodriver /usr/local/bin
Chrome
到https://chromedriver.storage.googleapis.com/index.html下载chromedriver,和firefox版一样复制到/usr/local/bin
sudo cp ~/Download/chromedriver /usr/local/bin
4. 检查安装
在python console
from selenium import webdriver browser = webdriver.Firefox() browser.get('https://www.baidu.com')
能够成功打开浏览器,并打开网页,说明安装成功。
note: 必须要保证系统里面安装了Firefox或者Chrome浏览器
不然可能会有如下的异常:
selenium.common.exceptions.WebDriverException: Message: Expected browser binary location, but unable to find binary in default location, no 'moz:firefoxOptions.binary' capability provided, and no binary flag set on the command line