Skip to content

Instantly share code, notes, and snippets.

@hjwp
Created July 30, 2013 09:14
Show Gist options
  • Save hjwp/6111474 to your computer and use it in GitHub Desktop.
Save hjwp/6111474 to your computer and use it in GitHub Desktop.
Selenium Python 3 patcher
#!/usr/bin/env python3
import difflib
import urllib.request
import os
import selenium
import imp
import shutil
target_dir = os.path.dirname(selenium.__file__)
for path, expected_num_lines in [("chrome/options.py", 11), ("firefox/firefox_binary.py", 36), ("remote/errorhandler.py", 15)]:
local_path, _ = urllib.request.urlretrieve("https://selenium.googlecode.com/git/py/selenium/webdriver/" + path)
target_path = os.path.join(target_dir, 'webdriver', path)
with open(local_path) as f:
local_contents = f.readlines()
with open(target_path) as f:
target_contents = f.readlines()
diff = list(difflib.unified_diff(target_contents, local_contents, fromfile=target_path, tofile=local_path))
assert len(diff) == expected_num_lines, 'diff did not have the expected number of lines. delete this assert and use this script at your own peril'
print('PATCHING')
print(''.join(diff))
try:
shutil.copy(local_path, target_path)
os.chmod(target_path, 0o644)
pass
except PermissionError:
print('script needs to be run with sudo')
print()
raise
# sanity-check
imp.reload(selenium)
from selenium.webdriver.remote import errorhandler
try:
errorhandler.ErrorHandler().check_response({'status': None, 'value': ''})
except NameError:
print('patching failed')
print()
raise
except selenium.common.exceptions.WebDriverException:
pass
print('All done')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment