Skip to content

Commit 0f06cfd

Browse files
committed
Add options for disabling Chromium features
1 parent 31020e4 commit 0f06cfd

File tree

10 files changed

+170
-21
lines changed

10 files changed

+170
-21
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,7 @@ pytest test_coffee_cart.py --trace
662662
--firefox-pref=SET # (Set a Firefox preference:value set, comma-separated.)
663663
--extension-zip=ZIP # (Load a Chrome Extension .zip|.crx, comma-separated.)
664664
--extension-dir=DIR # (Load a Chrome Extension directory, comma-separated.)
665+
--disable-features="F1,F2" # (Disable features, comma-separated, no spaces.)
665666
--binary-location=PATH # (Set path of the Chromium browser binary to use.)
666667
--driver-version=VER # (Set the chromedriver or uc_driver version to use.)
667668
--sjw # (Skip JS Waits for readyState to be "complete" or Angular to load.)

examples/raw_parameter_script.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
sb.window_size = None
8282
sb.maximize_option = False
8383
sb.visual_baseline = False
84+
sb.disable_features = None
8485
sb._disable_beforeunload = False
8586
sb.save_screenshot_after_test = False
8687
sb.no_screenshot_after_test = False

help_docs/customizing_test_runs.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ pytest my_first_test.py --settings-file=custom_settings.py
138138
--firefox-pref=SET # (Set a Firefox preference:value set, comma-separated.)
139139
--extension-zip=ZIP # (Load a Chrome Extension .zip|.crx, comma-separated.)
140140
--extension-dir=DIR # (Load a Chrome Extension directory, comma-separated.)
141+
--disable-features="F1,F2" # (Disable features, comma-separated, no spaces.)
141142
--binary-location=PATH # (Set path of the Chromium browser binary to use.)
142143
--driver-version=VER # (Set the chromedriver or uc_driver version to use.)
143144
--sjw # (Skip JS Waits for readyState to be "complete" or Angular to load.)

seleniumbase/behave/behave_sb.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ def get_configured_sb(context):
226226
sb.chromium_arg = None
227227
sb.firefox_arg = None
228228
sb.firefox_pref = None
229+
sb.disable_features = None
229230
sb.proxy_string = None
230231
sb.proxy_bypass_list = None
231232
sb.proxy_pac_url = None
@@ -748,6 +749,13 @@ def get_configured_sb(context):
748749
firefox_pref = sb.firefox_pref # revert to default
749750
sb.firefox_pref = firefox_pref
750751
continue
752+
# Handle: -D disable-features="F1,F2" / disable_features="F1,F2"
753+
if low_key in ["disable-features", "disable_features"]:
754+
disable_features = userdata[key]
755+
if disable_features == "true":
756+
disable_features = sb.disable_features # revert to default
757+
sb.disable_features = disable_features
758+
continue
751759
# Handle: -D proxy=SERVER:PORT / proxy=USERNAME:PASSWORD@SERVER:PORT
752760
if low_key in ["proxy", "proxy-server", "proxy-string"]:
753761
proxy_string = userdata[key]

seleniumbase/core/browser_launcher.py

Lines changed: 75 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,7 @@ def _set_chrome_options(
763763
user_data_dir,
764764
extension_zip,
765765
extension_dir,
766+
disable_features,
766767
binary_location,
767768
driver_version,
768769
page_load_strategy,
@@ -1044,6 +1045,7 @@ def _set_chrome_options(
10441045
binary_loc = detect_b_ver.get_binary_location(br_app, True)
10451046
if os.path.exists(binary_loc):
10461047
binary_location = binary_loc
1048+
extra_disabled_features = []
10471049
if chromium_arg:
10481050
# Can be a comma-separated list of Chromium args
10491051
chromium_arg_list = chromium_arg.split(",")
@@ -1069,8 +1071,13 @@ def _set_chrome_options(
10691071
)
10701072
if os.path.exists(binary_loc):
10711073
binary_location = binary_loc
1074+
elif "disable-features=" in chromium_arg_item:
1075+
d_f = chromium_arg_item.split("disable-features=")[-1]
1076+
extra_disabled_features.append(d_f)
10721077
elif len(chromium_arg_item) >= 3:
10731078
chrome_options.add_argument(chromium_arg_item)
1079+
if disable_features:
1080+
extra_disabled_features.extend(disable_features.split(","))
10741081
if devtools and not headless:
10751082
chrome_options.add_argument("--auto-open-devtools-for-tabs")
10761083
if user_agent:
@@ -1089,19 +1096,36 @@ def _set_chrome_options(
10891096
if headless or headless2 or is_using_uc(undetectable, browser_name):
10901097
chrome_options.add_argument("--disable-renderer-backgrounding")
10911098
chrome_options.add_argument("--disable-backgrounding-occluded-windows")
1099+
chrome_options.add_argument("--disable-client-side-phishing-detection")
1100+
chrome_options.add_argument("--disable-oopr-debug-crash-dump")
1101+
chrome_options.add_argument("--disable-top-sites")
10921102
chrome_options.add_argument("--ash-no-nudges")
1103+
chrome_options.add_argument("--no-crash-upload")
10931104
chrome_options.add_argument("--deny-permission-prompts")
1105+
included_disabled_features = []
10941106
if user_data_dir:
1095-
chrome_options.add_argument(
1096-
"--disable-features=OptimizationHintsFetching,Translate,"
1097-
"OptimizationTargetPrediction,PrivacySandboxSettings4,"
1098-
"DownloadBubble,DownloadBubbleV2"
1099-
)
1107+
included_disabled_features.append("OptimizationHintsFetching")
1108+
included_disabled_features.append("Translate")
1109+
included_disabled_features.append("OptimizationTargetPrediction")
1110+
included_disabled_features.append("PrivacySandboxSettings4")
1111+
included_disabled_features.append("DownloadBubble")
1112+
included_disabled_features.append("DownloadBubbleV2")
1113+
for item in extra_disabled_features:
1114+
if item not in included_disabled_features:
1115+
included_disabled_features.append(item)
1116+
d_f_string = ",".join(included_disabled_features)
1117+
chrome_options.add_argument("--disable-features=%s" % d_f_string)
11001118
else:
1101-
chrome_options.add_argument(
1102-
"--disable-features=OptimizationHintsFetching,Translate,"
1103-
"OptimizationTargetPrediction,DownloadBubble,DownloadBubbleV2"
1104-
)
1119+
included_disabled_features.append("OptimizationHintsFetching")
1120+
included_disabled_features.append("Translate")
1121+
included_disabled_features.append("OptimizationTargetPrediction")
1122+
included_disabled_features.append("DownloadBubble")
1123+
included_disabled_features.append("DownloadBubbleV2")
1124+
for item in extra_disabled_features:
1125+
if item not in included_disabled_features:
1126+
included_disabled_features.append(item)
1127+
d_f_string = ",".join(included_disabled_features)
1128+
chrome_options.add_argument("--disable-features=%s" % d_f_string)
11051129
if (
11061130
is_using_uc(undetectable, browser_name)
11071131
and (
@@ -1338,6 +1362,7 @@ def get_driver(
13381362
user_data_dir=None,
13391363
extension_zip=None,
13401364
extension_dir=None,
1365+
disable_features=None,
13411366
binary_location=None,
13421367
driver_version=None,
13431368
page_load_strategy=None,
@@ -1550,6 +1575,7 @@ def get_driver(
15501575
user_data_dir,
15511576
extension_zip,
15521577
extension_dir,
1578+
disable_features,
15531579
binary_location,
15541580
driver_version,
15551581
page_load_strategy,
@@ -1605,6 +1631,7 @@ def get_driver(
16051631
user_data_dir,
16061632
extension_zip,
16071633
extension_dir,
1634+
disable_features,
16081635
binary_location,
16091636
driver_version,
16101637
page_load_strategy,
@@ -1664,6 +1691,7 @@ def get_remote_driver(
16641691
user_data_dir,
16651692
extension_zip,
16661693
extension_dir,
1694+
disable_features,
16671695
binary_location,
16681696
driver_version,
16691697
page_load_strategy,
@@ -1784,6 +1812,7 @@ def get_remote_driver(
17841812
user_data_dir,
17851813
extension_zip,
17861814
extension_dir,
1815+
disable_features,
17871816
binary_location,
17881817
driver_version,
17891818
page_load_strategy,
@@ -1955,6 +1984,7 @@ def get_remote_driver(
19551984
user_data_dir,
19561985
extension_zip,
19571986
extension_dir,
1987+
disable_features,
19581988
binary_location,
19591989
driver_version,
19601990
page_load_strategy,
@@ -2075,6 +2105,7 @@ def get_local_driver(
20752105
user_data_dir,
20762106
extension_zip,
20772107
extension_dir,
2108+
disable_features,
20782109
binary_location,
20792110
driver_version,
20802111
page_load_strategy,
@@ -2570,18 +2601,6 @@ def get_local_driver(
25702601
edge_options.add_argument(
25712602
"--disable-autofill-keyboard-accessory-view[8]"
25722603
)
2573-
edge_options.add_argument("--ash-no-nudges")
2574-
edge_options.add_argument("--deny-permission-prompts")
2575-
if user_data_dir:
2576-
edge_options.add_argument(
2577-
"--disable-features=OptimizationHintsFetching,Translate,"
2578-
"OptimizationTargetPrediction,PrivacySandboxSettings4"
2579-
)
2580-
else:
2581-
edge_options.add_argument(
2582-
"--disable-features=OptimizationHintsFetching,Translate,"
2583-
"OptimizationTargetPrediction"
2584-
)
25852604
edge_options.add_argument("--disable-browser-side-navigation")
25862605
edge_options.add_argument("--disable-translate")
25872606
if not enable_ws:
@@ -2596,6 +2615,12 @@ def get_local_driver(
25962615
if headless or headless2 or is_using_uc(undetectable, browser_name):
25972616
edge_options.add_argument("--disable-renderer-backgrounding")
25982617
edge_options.add_argument("--disable-backgrounding-occluded-windows")
2618+
edge_options.add_argument("--disable-client-side-phishing-detection")
2619+
edge_options.add_argument("--disable-oopr-debug-crash-dump")
2620+
edge_options.add_argument("--disable-top-sites")
2621+
edge_options.add_argument("--ash-no-nudges")
2622+
edge_options.add_argument("--no-crash-upload")
2623+
edge_options.add_argument("--deny-permission-prompts")
25992624
if (
26002625
page_load_strategy
26012626
and page_load_strategy.lower() in ["eager", "none"]
@@ -2677,6 +2702,7 @@ def get_local_driver(
26772702
edge_options.add_argument("--disable-gpu")
26782703
if IS_LINUX:
26792704
edge_options.add_argument("--disable-dev-shm-usage")
2705+
extra_disabled_features = []
26802706
set_binary = False
26812707
if chromium_arg:
26822708
# Can be a comma-separated list of Chromium args
@@ -2690,8 +2716,33 @@ def get_local_driver(
26902716
chromium_arg_item = "--" + chromium_arg_item
26912717
if "set-binary" in chromium_arg_item:
26922718
set_binary = True
2719+
elif "disable-features=" in chromium_arg_item:
2720+
d_f = chromium_arg_item.split("disable-features=")[-1]
2721+
extra_disabled_features.append(d_f)
26932722
elif len(chromium_arg_item) >= 3:
26942723
edge_options.add_argument(chromium_arg_item)
2724+
if disable_features:
2725+
extra_disabled_features.extend(disable_features.split(","))
2726+
included_disabled_features = []
2727+
if user_data_dir:
2728+
included_disabled_features.append("OptimizationHintsFetching")
2729+
included_disabled_features.append("Translate")
2730+
included_disabled_features.append("OptimizationTargetPrediction")
2731+
included_disabled_features.append("PrivacySandboxSettings4")
2732+
for item in extra_disabled_features:
2733+
if item not in included_disabled_features:
2734+
included_disabled_features.append(item)
2735+
d_f_string = ",".join(included_disabled_features)
2736+
edge_options.add_argument("--disable-features=%s" % d_f_string)
2737+
else:
2738+
included_disabled_features.append("OptimizationHintsFetching")
2739+
included_disabled_features.append("Translate")
2740+
included_disabled_features.append("OptimizationTargetPrediction")
2741+
for item in extra_disabled_features:
2742+
if item not in included_disabled_features:
2743+
included_disabled_features.append(item)
2744+
d_f_string = ",".join(included_disabled_features)
2745+
edge_options.add_argument("--disable-features=%s" % d_f_string)
26952746
if (set_binary or IS_LINUX) and not binary_location:
26962747
br_app = "edge"
26972748
binary_loc = detect_b_ver.get_binary_location(br_app)
@@ -2831,6 +2882,7 @@ def get_local_driver(
28312882
user_data_dir,
28322883
extension_zip,
28332884
extension_dir,
2885+
disable_features,
28342886
binary_location,
28352887
driver_version,
28362888
page_load_strategy,
@@ -3348,6 +3400,7 @@ def get_local_driver(
33483400
None, # user_data_dir
33493401
None, # extension_zip
33503402
None, # extension_dir
3403+
None, # disable_features
33513404
binary_location,
33523405
driver_version,
33533406
page_load_strategy,
@@ -3565,6 +3618,7 @@ def get_local_driver(
35653618
None, # user_data_dir
35663619
None, # extension_zip
35673620
None, # extension_dir
3621+
None, # disable_features
35683622
binary_location,
35693623
driver_version,
35703624
page_load_strategy,

seleniumbase/fixtures/base_case.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3777,6 +3777,7 @@ def get_new_driver(
37773777
user_data_dir=None,
37783778
extension_zip=None,
37793779
extension_dir=None,
3780+
disable_features=None,
37803781
binary_location=None,
37813782
driver_version=None,
37823783
page_load_strategy=None,
@@ -3835,6 +3836,7 @@ def get_new_driver(
38353836
user_data_dir - Chrome's User Data Directory to use (Chrome-only)
38363837
extension_zip - A Chrome Extension ZIP file to use (Chrome-only)
38373838
extension_dir - A Chrome Extension folder to use (Chrome-only)
3839+
disable_features - the option to disable features on Chrome/Edge
38383840
binary_location - the path of the browser binary to use (Chromium)
38393841
driver_version - the chromedriver or uc_driver version to force
38403842
page_load_strategy - the option to change pageLoadStrategy (Chrome)
@@ -3960,6 +3962,8 @@ def get_new_driver(
39603962
extension_zip = self.extension_zip
39613963
if extension_dir is None:
39623964
extension_dir = self.extension_dir
3965+
if disable_features is None:
3966+
disable_features = self.disable_features
39633967
if binary_location is None:
39643968
binary_location = self.binary_location
39653969
if driver_version is None:
@@ -4040,6 +4044,7 @@ def get_new_driver(
40404044
user_data_dir=user_data_dir,
40414045
extension_zip=extension_zip,
40424046
extension_dir=extension_dir,
4047+
disable_features=disable_features,
40434048
binary_location=binary_location,
40444049
driver_version=driver_version,
40454050
page_load_strategy=page_load_strategy,
@@ -14331,6 +14336,7 @@ def setUp(self, masterqa_mode=False):
1433114336
self.user_data_dir = sb_config.user_data_dir
1433214337
self.extension_zip = sb_config.extension_zip
1433314338
self.extension_dir = sb_config.extension_dir
14339+
self.disable_features = sb_config.disable_features
1433414340
self.binary_location = sb_config.binary_location
1433514341
self.driver_version = sb_config.driver_version
1433614342
self.page_load_strategy = sb_config.page_load_strategy
@@ -14652,6 +14658,7 @@ def setUp(self, masterqa_mode=False):
1465214658
user_data_dir=self.user_data_dir,
1465314659
extension_zip=self.extension_zip,
1465414660
extension_dir=self.extension_dir,
14661+
disable_features=self.disable_features,
1465514662
binary_location=self.binary_location,
1465614663
driver_version=self.driver_version,
1465714664
page_load_strategy=self.page_load_strategy,

seleniumbase/plugins/driver_manager.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ def Driver(
108108
user_data_dir=None, # Set the Chrome user data directory to use.
109109
extension_zip=None, # Load a Chrome Extension .zip|.crx, comma-separated.
110110
extension_dir=None, # Load a Chrome Extension directory, comma-separated.
111+
disable_features=None, # "F1,F2" (Disable Chrome features, ","-separated.)
111112
binary_location=None, # Set path of the Chromium browser binary to use.
112113
driver_version=None, # Set the chromedriver or uc_driver version to use.
113114
page_load_strategy=None, # Set Chrome PLS to "normal", "eager", or "none".
@@ -264,6 +265,30 @@ def Driver(
264265
proxy_string = proxy_string[1:-1]
265266
elif proxy_string.startswith("'") and proxy_string.endswith("'"):
266267
proxy_string = proxy_string[1:-1]
268+
c_a = chromium_arg
269+
if c_a is None and "--chromium-arg" in arg_join:
270+
if "--chromium-arg=" in arg_join:
271+
c_a = arg_join.split("--chromium-arg=")[1].split(" ")[0]
272+
elif "--chromium-arg " in arg_join:
273+
c_a = arg_join.split("--chromium-arg ")[1].split(" ")[0]
274+
if c_a:
275+
if c_a.startswith('"') and c_a.endswith('"'):
276+
c_a = c_a[1:-1]
277+
elif c_a.startswith("'") and c_a.endswith("'"):
278+
c_a = c_a[1:-1]
279+
chromium_arg = c_a
280+
d_f = disable_features
281+
if d_f is None and "--disable-features" in arg_join:
282+
if "--disable-features=" in arg_join:
283+
d_f = arg_join.split("--disable-features=")[1].split(" ")[0]
284+
elif "--disable-features " in arg_join:
285+
d_f = arg_join.split("--disable-features ")[1].split(" ")[0]
286+
if d_f:
287+
if d_f.startswith('"') and d_f.endswith('"'):
288+
d_f = d_f[1:-1]
289+
elif c_a.startswith("'") and d_f.endswith("'"):
290+
d_f = d_f[1:-1]
291+
disable_features = d_f
267292
user_agent = agent
268293
recorder_mode = False
269294
if recorder_ext:
@@ -505,6 +530,7 @@ def Driver(
505530
user_data_dir=user_data_dir,
506531
extension_zip=extension_zip,
507532
extension_dir=extension_dir,
533+
disable_features=disable_features,
508534
binary_location=binary_location,
509535
driver_version=driver_version,
510536
page_load_strategy=page_load_strategy,

0 commit comments

Comments
 (0)