Skip to content

Commit bd2754a

Browse files
committed
Transform the "ad-block" feature into a Chromium extension
1 parent 1af6a98 commit bd2754a

File tree

3 files changed

+35
-10
lines changed

3 files changed

+35
-10
lines changed

seleniumbase/core/browser_launcher.py

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
os.environ["PATH"] = DRIVER_DIR + os.pathsep + os.environ["PATH"]
2929
EXTENSIONS_DIR = os.path.dirname(os.path.realpath(extensions.__file__))
3030
DISABLE_CSP_ZIP_PATH = "%s/%s" % (EXTENSIONS_DIR, "disable_csp.zip")
31+
AD_BLOCK_ZIP_PATH = "%s/%s" % (EXTENSIONS_DIR, "ad_block.zip")
3132
RECORDER_ZIP_PATH = "%s/%s" % (EXTENSIONS_DIR, "recorder.zip")
3233
PROXY_ZIP_PATH = proxy_helper.PROXY_ZIP_PATH
3334
PROXY_ZIP_PATH_2 = proxy_helper.PROXY_ZIP_PATH_2
@@ -199,8 +200,17 @@ def _add_chrome_disable_csp_extension(chrome_options):
199200
return chrome_options
200201

201202

203+
def _add_chrome_ad_block_extension(chrome_options):
204+
"""Block Ads on Chromium Browsers with a browser extension.
205+
See https://github.com/slingamn/simpleblock for details."""
206+
ad_block_zip = AD_BLOCK_ZIP_PATH
207+
chrome_options.add_extension(ad_block_zip)
208+
return chrome_options
209+
210+
202211
def _add_chrome_recorder_extension(chrome_options):
203-
"""The SeleniumBase Recorder Chromium extension."""
212+
"""The SeleniumBase Recorder Chrome/Edge extension.
213+
https://seleniumbase.io/help_docs/recorder_mode/"""
204214
recorder_zip = RECORDER_ZIP_PATH
205215
chrome_options.add_extension(recorder_zip)
206216
return chrome_options
@@ -228,6 +238,7 @@ def _set_chrome_options(
228238
devtools,
229239
remote_debug,
230240
swiftshader,
241+
ad_block_on,
231242
block_images,
232243
chromium_arg,
233244
user_data_dir,
@@ -322,10 +333,10 @@ def _set_chrome_options(
322333
chrome_options.add_experimental_option(
323334
"mobileEmulation", emulator_settings
324335
)
325-
chrome_options.add_argument("--enable-sync")
326336
if (
327337
not proxy_auth
328338
and not disable_csp
339+
and not ad_block_on
329340
and not recorder_ext
330341
and (not extension_zip and not extension_dir)
331342
):
@@ -391,7 +402,8 @@ def _set_chrome_options(
391402
# Headless Chrome doesn't support extensions, which are required
392403
# for disabling the Content Security Policy on Chrome
393404
chrome_options = _add_chrome_disable_csp_extension(chrome_options)
394-
chrome_options.add_argument("--enable-sync")
405+
if ad_block_on and not headless:
406+
chrome_options = _add_chrome_ad_block_extension(chrome_options)
395407
if recorder_ext and not headless:
396408
chrome_options = _add_chrome_recorder_extension(chrome_options)
397409
if proxy_string:
@@ -687,6 +699,7 @@ def get_driver(
687699
devtools=None,
688700
remote_debug=None,
689701
swiftshader=None,
702+
ad_block_on=None,
690703
block_images=None,
691704
chromium_arg=None,
692705
firefox_arg=None,
@@ -760,6 +773,7 @@ def get_driver(
760773
devtools,
761774
remote_debug,
762775
swiftshader,
776+
ad_block_on,
763777
block_images,
764778
chromium_arg,
765779
firefox_arg,
@@ -796,6 +810,7 @@ def get_driver(
796810
devtools,
797811
remote_debug,
798812
swiftshader,
813+
ad_block_on,
799814
block_images,
800815
chromium_arg,
801816
firefox_arg,
@@ -836,6 +851,7 @@ def get_remote_driver(
836851
devtools,
837852
remote_debug,
838853
swiftshader,
854+
ad_block_on,
839855
block_images,
840856
chromium_arg,
841857
firefox_arg,
@@ -900,6 +916,7 @@ def get_remote_driver(
900916
devtools,
901917
remote_debug,
902918
swiftshader,
919+
ad_block_on,
903920
block_images,
904921
chromium_arg,
905922
user_data_dir,
@@ -1053,6 +1070,7 @@ def get_local_driver(
10531070
devtools,
10541071
remote_debug,
10551072
swiftshader,
1073+
ad_block_on,
10561074
block_images,
10571075
chromium_arg,
10581076
firefox_arg,
@@ -1220,6 +1238,7 @@ def get_local_driver(
12201238
devtools,
12211239
remote_debug,
12221240
swiftshader,
1241+
ad_block_on,
12231242
block_images,
12241243
chromium_arg,
12251244
user_data_dir,
@@ -1307,7 +1326,6 @@ def get_local_driver(
13071326
edge_options.add_experimental_option(
13081327
"mobileEmulation", emulator_settings
13091328
)
1310-
edge_options.add_argument("--enable-sync")
13111329
if user_data_dir:
13121330
abs_path = os.path.abspath(user_data_dir)
13131331
edge_options.add_argument("user-data-dir=%s" % abs_path)
@@ -1342,10 +1360,10 @@ def get_local_driver(
13421360
# Headless Edge doesn't support extensions, which are required
13431361
# for disabling the Content Security Policy on Edge
13441362
edge_options = _add_chrome_disable_csp_extension(edge_options)
1345-
edge_options.add_argument("--enable-sync")
1363+
if ad_block_on and not headless:
1364+
edge_options = _add_chrome_ad_block_extension(edge_options)
13461365
if recorder_ext and not headless:
13471366
edge_options = _add_chrome_recorder_extension(edge_options)
1348-
edge_options.add_argument("--enable-sync")
13491367
if proxy_string:
13501368
if proxy_auth:
13511369
edge_options = _add_chrome_proxy_extension(
@@ -1431,6 +1449,7 @@ def get_local_driver(
14311449
devtools,
14321450
remote_debug,
14331451
swiftshader,
1452+
ad_block_on,
14341453
block_images,
14351454
chromium_arg,
14361455
user_data_dir,
@@ -1475,6 +1494,7 @@ def get_local_driver(
14751494
devtools,
14761495
remote_debug,
14771496
swiftshader,
1497+
ad_block_on,
14781498
block_images,
14791499
chromium_arg,
14801500
user_data_dir,
@@ -1556,6 +1576,7 @@ def get_local_driver(
15561576
devtools,
15571577
remote_debug,
15581578
swiftshader,
1579+
ad_block_on,
15591580
block_images,
15601581
chromium_arg,
15611582
user_data_dir,

seleniumbase/extensions/ad_block.zip

11.7 KB
Binary file not shown.

seleniumbase/fixtures/base_case.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2480,6 +2480,7 @@ def get_new_driver(
24802480
devtools=None,
24812481
remote_debug=None,
24822482
swiftshader=None,
2483+
ad_block_on=None,
24832484
block_images=None,
24842485
chromium_arg=None,
24852486
firefox_arg=None,
@@ -2519,6 +2520,7 @@ def get_new_driver(
25192520
devtools - the option to open Chrome's DevTools on start (Chrome)
25202521
remote_debug - the option to enable Chrome's Remote Debugger
25212522
swiftshader - the option to use Chrome's swiftshader (Chrome-only)
2523+
ad_block_on - the option to block ads from loading (Chromium-only)
25222524
block_images - the option to block images from loading (Chrome)
25232525
chromium_arg - the option to add a Chromium arg to Chrome/Edge
25242526
firefox_arg - the option to add a Firefox arg to Firefox runs
@@ -2604,6 +2606,8 @@ def get_new_driver(
26042606
remote_debug = self.remote_debug
26052607
if swiftshader is None:
26062608
swiftshader = self.swiftshader
2609+
if ad_block_on is None:
2610+
ad_block_on = self.ad_block_on
26072611
if block_images is None:
26082612
block_images = self.block_images
26092613
if chromium_arg is None:
@@ -2664,6 +2668,7 @@ def get_new_driver(
26642668
devtools=devtools,
26652669
remote_debug=remote_debug,
26662670
swiftshader=swiftshader,
2671+
ad_block_on=ad_block_on,
26672672
block_images=block_images,
26682673
chromium_arg=chromium_arg,
26692674
firefox_arg=firefox_arg,
@@ -2891,13 +2896,11 @@ def wait_for_ready_state_complete(self, timeout=None):
28912896
self.wait_for_angularjs(timeout=settings.MINI_TIMEOUT)
28922897
if self.js_checking_on:
28932898
self.assert_no_js_errors()
2894-
if self.ad_block_on:
2895-
# If the ad_block feature is enabled, then block ads for new URLs
2899+
if self.ad_block_on and (self.headless or not self.is_chromium()):
2900+
# For Chromium browsers in headed mode, the extension is used
28962901
current_url = self.get_current_url()
28972902
if not current_url == self.__last_page_load_url:
2898-
time.sleep(0.02)
28992903
self.ad_block()
2900-
time.sleep(0.02)
29012904
if self.is_element_present("iframe"):
29022905
time.sleep(0.1) # iframe ads take slightly longer to load
29032906
self.ad_block() # Do ad_block on slower-loading iframes
@@ -9944,6 +9947,7 @@ def setUp(self, masterqa_mode=False):
99449947
devtools=self.devtools,
99459948
remote_debug=self.remote_debug,
99469949
swiftshader=self.swiftshader,
9950+
ad_block_on=self.ad_block_on,
99479951
block_images=self.block_images,
99489952
chromium_arg=self.chromium_arg,
99499953
firefox_arg=self.firefox_arg,

0 commit comments

Comments
 (0)