Skip to content

Commit 68a7e27

Browse files
committed
Auto-repair ChromeDriver to sync with Chrome (Mac/Win only)
1 parent a18511e commit 68a7e27

File tree

1 file changed

+131
-1
lines changed

1 file changed

+131
-1
lines changed

seleniumbase/core/browser_launcher.py

Lines changed: 131 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,57 @@ def is_headless_iedriver_on_path():
108108
return os.path.exists(LOCAL_HEADLESS_IEDRIVER)
109109

110110

111+
def _repair_chromedriver(chrome_options, headless_options):
112+
import subprocess
113+
114+
driver = None
115+
subprocess.check_call(
116+
"sbase install chromedriver 2.44", shell=True
117+
)
118+
try:
119+
driver = webdriver.Chrome(options=headless_options)
120+
except Exception:
121+
subprocess.check_call(
122+
"sbase install chromedriver latest-1", shell=True
123+
)
124+
return
125+
chrome_version = None
126+
if "version" in driver.capabilities:
127+
chrome_version = driver.capabilities["version"]
128+
else:
129+
chrome_version = driver.capabilities["browserVersion"]
130+
major_chrome_ver = chrome_version.split(".")[0]
131+
chrome_dict = driver.capabilities["chrome"]
132+
driver.quit()
133+
chromedriver_ver = chrome_dict["chromedriverVersion"]
134+
chromedriver_ver = chromedriver_ver.split(" ")[0]
135+
major_chromedriver_ver = chromedriver_ver.split(".")[0]
136+
if major_chromedriver_ver != major_chrome_ver:
137+
subprocess.check_call(
138+
"sbase install chromedriver %s" % major_chrome_ver,
139+
shell=True
140+
)
141+
return
142+
143+
144+
def _mark_chromedriver_repaired():
145+
import codecs
146+
147+
abs_path = os.path.abspath(".")
148+
chromedriver_repaired_lock = constants.MultiBrowser.CHROMEDRIVER_REPAIRED
149+
file_path = os.path.join(abs_path, chromedriver_repaired_lock)
150+
out_file = codecs.open(file_path, "w+", encoding="utf-8")
151+
out_file.writelines("")
152+
out_file.close()
153+
154+
155+
def _was_chromedriver_repaired():
156+
abs_path = os.path.abspath(".")
157+
chromedriver_repaired_lock = constants.MultiBrowser.CHROMEDRIVER_REPAIRED
158+
file_path = os.path.join(abs_path, chromedriver_repaired_lock)
159+
return os.path.exists(file_path)
160+
161+
111162
def _add_chrome_proxy_extension(
112163
chrome_options, proxy_string, proxy_user, proxy_pass
113164
):
@@ -1426,8 +1477,87 @@ def get_local_driver(
14261477
print("\nWarning: chromedriver not found. Installing now:")
14271478
sb_install.main(override="chromedriver")
14281479
sys.argv = sys_args # Put back the original sys args
1480+
else:
1481+
import fasteners
1482+
from seleniumbase.console_scripts import sb_install
1483+
1484+
chromedriver_fixing_lock = fasteners.InterProcessLock(
1485+
constants.MultiBrowser.CHROMEDRIVER_FIXING_LOCK
1486+
)
1487+
with chromedriver_fixing_lock:
1488+
if not is_chromedriver_on_path():
1489+
sys_args = sys.argv # Save a copy of sys args
1490+
print(
1491+
"\nWarning: chromedriver not found. "
1492+
"Installing now:"
1493+
)
1494+
sb_install.main(override="chromedriver")
1495+
sys.argv = sys_args # Put back original sys args
14291496
if not headless or "linux" not in PLATFORM:
1430-
return webdriver.Chrome(options=chrome_options)
1497+
try:
1498+
driver = webdriver.Chrome(options=chrome_options)
1499+
except Exception as e:
1500+
auto_upgrade_chromedriver = False
1501+
if "This version of ChromeDriver only supports" in e.msg:
1502+
auto_upgrade_chromedriver = True
1503+
if not auto_upgrade_chromedriver:
1504+
raise Exception(e.msg) # Not an obvious fix. Raise.
1505+
else:
1506+
pass # Try upgrading ChromeDriver to match Chrome.
1507+
headless = True
1508+
headless_options = _set_chrome_options(
1509+
browser_name,
1510+
downloads_path,
1511+
headless,
1512+
locale_code,
1513+
proxy_string,
1514+
proxy_auth,
1515+
proxy_user,
1516+
proxy_pass,
1517+
user_agent,
1518+
disable_csp,
1519+
enable_ws,
1520+
enable_sync,
1521+
use_auto_ext,
1522+
no_sandbox,
1523+
disable_gpu,
1524+
incognito,
1525+
guest_mode,
1526+
devtools,
1527+
remote_debug,
1528+
swiftshader,
1529+
block_images,
1530+
chromium_arg,
1531+
user_data_dir,
1532+
extension_zip,
1533+
extension_dir,
1534+
servername,
1535+
mobile_emulator,
1536+
device_width,
1537+
device_height,
1538+
device_pixel_ratio,
1539+
)
1540+
args = " ".join(sys.argv)
1541+
if ("-n" in sys.argv or " -n=" in args or args == "-c"):
1542+
import fasteners
1543+
1544+
chromedriver_fixing_lock = fasteners.InterProcessLock(
1545+
constants.MultiBrowser.CHROMEDRIVER_FIXING_LOCK
1546+
)
1547+
with chromedriver_fixing_lock:
1548+
if not _was_chromedriver_repaired():
1549+
_repair_chromedriver(
1550+
chrome_options, headless_options
1551+
)
1552+
_mark_chromedriver_repaired()
1553+
else:
1554+
if not _was_chromedriver_repaired():
1555+
_repair_chromedriver(
1556+
chrome_options, headless_options
1557+
)
1558+
_mark_chromedriver_repaired()
1559+
driver = webdriver.Chrome(options=chrome_options)
1560+
return driver
14311561
else: # Running headless on Linux
14321562
try:
14331563
return webdriver.Chrome(options=chrome_options)

0 commit comments

Comments
 (0)