Last active
October 28, 2020 16:58
-
-
Save nealcaren/e617cfac23a0bd6c00adbfed3b473dbb to your computer and use it in GitHub Desktop.
Install Selenium and the appropriate Chrome driver for your Mac using conda.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import re | |
import plistlib | |
# Find the available Chrome Drivers | |
url = "https://pypi.org/project/chromedriver-binary/#history" | |
html = requests.get(url).text | |
available = re.findall("\/project\/chromedriver-binary\/(.*?)\/", html) | |
available = [i for i in available if i.count(".") > 2] | |
# See what version of Chrome you have | |
plistloc = "/Applications/Google Chrome.app/Contents/Info.plist" | |
with open(plistloc, "rb") as infile: | |
pl = plistlib.load(infile) | |
version = pl["CFBundleShortVersionString"] + ".0" | |
def first3(string): | |
return "".join(string.split(".")[:3]) | |
def find_version(item, available): | |
for item in available: | |
if first3(item) == first3(version): | |
return item | |
return "No Match" | |
# Find an available driver version close to your install version | |
best_fit = find_version(item, available) | |
print("Your Chrome Version: %s" % version) | |
print("Installing the closest driver version: %s" % best_fit) | |
# Install that version | |
%conda install -c conda-forge selenium python-chromedriver-binary==$best_fit | |
# Confirm that it works | |
from selenium import webdriver | |
import chromedriver_binary | |
driver = webdriver.Chrome() | |
driver.get('http://www.google.com') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment