Skip to content

Commit ed48c43

Browse files
committed
Properly parse preferences with undetected-chromedriver
1 parent dea5672 commit ed48c43

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

seleniumbase/undetected/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,10 @@ def __init__(
237237
mode="r+",
238238
) as fs:
239239
config = json.load(fs)
240-
if config["profile"]["exit_type"] is not None:
240+
if (
241+
"exit_type" not in config["profile"].keys()
242+
or config["profile"]["exit_type"] is not None
243+
):
241244
config["profile"]["exit_type"] = None
242245
fs.seek(0, 0)
243246
json.dump(config, fs)

seleniumbase/undetected/options.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@ def _undot_key(key, value):
2626
value = ChromeOptions._undot_key(rest, value)
2727
return {key: value}
2828

29+
@staticmethod
30+
def _merge_nested(a, b):
31+
"""Merges b into a, overwriting duplicate leaf values using b."""
32+
for key in b:
33+
if key in a:
34+
if isinstance(a[key], dict) and isinstance(b[key], dict):
35+
ChromeOptions._merge_nested(a[key], b[key])
36+
continue
37+
a[key] = b[key]
38+
return a
39+
2940
def handle_prefs(self, user_data_dir):
3041
prefs = self.experimental_options.get("prefs")
3142
if prefs:
@@ -34,11 +45,13 @@ def handle_prefs(self, user_data_dir):
3445
os.makedirs(default_path, exist_ok=True)
3546
undot_prefs = {}
3647
for key, value in prefs.items():
37-
undot_prefs.update(self._undot_key(key, value))
48+
undot_prefs = self._merge_nested(
49+
undot_prefs, self._undot_key(key, value)
50+
)
3851
prefs_file = os.path.join(default_path, "Preferences")
3952
if os.path.exists(prefs_file):
4053
with open(prefs_file, encoding="latin1", mode="r") as f:
41-
undot_prefs.update(json.load(f))
54+
undot_prefs = self._merge_nested(json.load(f), undot_prefs)
4255
with open(prefs_file, encoding="latin1", mode="w") as f:
4356
json.dump(undot_prefs, f)
4457
# Remove experimental_options to avoid errors

0 commit comments

Comments
 (0)