1010import textwrap
1111import time
1212import yaml
13+ from distutils import spawn
1314
1415from shot_scraper .utils import filename_for_url , url_or_file_path
1516
@@ -40,6 +41,25 @@ def reduced_motion_option(fn):
4041 )(fn )
4142 return fn
4243
44+ def system_browser_option (fn ):
45+ click .option (
46+ "--system-browser" ,
47+ is_flag = True ,
48+ help = "Use web browser installed by the system"
49+ )(fn )
50+ return fn
51+
52+ def browser_args_option (fn ):
53+ click .option ("--browser-args" , help = "Browser command-line arguments" )(fn )
54+ return fn
55+
56+ def ignore_https_errors_option (fn ):
57+ click .option (
58+ "--ignore-https-errors" ,
59+ is_flag = True ,
60+ help = "Ignore HTTPS errors"
61+ )(fn )
62+ return fn
4363
4464@click .group (
4565 cls = DefaultGroup ,
@@ -142,6 +162,9 @@ def cli():
142162@browser_option
143163@user_agent_option
144164@reduced_motion_option
165+ @system_browser_option
166+ @browser_args_option
167+ @ignore_https_errors_option
145168def shot (
146169 url ,
147170 auth ,
@@ -165,6 +188,9 @@ def shot(
165188 browser ,
166189 user_agent ,
167190 reduced_motion ,
191+ system_browser ,
192+ browser_args ,
193+ ignore_https_errors ,
168194):
169195 """
170196 Take a single screenshot of a page or portion of a page.
@@ -224,6 +250,9 @@ def shot(
224250 user_agent = user_agent ,
225251 timeout = timeout ,
226252 reduced_motion = reduced_motion ,
253+ system_browser = system_browser ,
254+ browser_args = browser_args ,
255+ ignore_https_errors = ignore_https_errors ,
227256 )
228257 if interactive or devtools :
229258 use_existing_page = True
@@ -267,8 +296,15 @@ def _browser_context(
267296 user_agent = None ,
268297 timeout = None ,
269298 reduced_motion = False ,
299+ system_browser = False ,
300+ browser_args = None ,
301+ ignore_https_errors = None ,
270302):
271303 browser_kwargs = dict (headless = not interactive , devtools = devtools )
304+ if system_browser :
305+ browser_kwargs ['executable_path' ] = spawn .find_executable (browser )
306+ if browser_args :
307+ browser_kwargs ["args" ] = browser_args .split (' ' )
272308 if browser == "chromium" :
273309 browser_obj = p .chromium .launch (** browser_kwargs )
274310 elif browser == "firefox" :
@@ -287,6 +323,8 @@ def _browser_context(
287323 context_args ["reduced_motion" ] = "reduce"
288324 if user_agent is not None :
289325 context_args ["user_agent" ] = user_agent
326+ if ignore_https_errors is not None :
327+ context_args ["ignore_https_errors" ] = ignore_https_errors
290328 context = browser_obj .new_context (** context_args )
291329 if timeout :
292330 context .set_default_timeout (timeout )
@@ -318,6 +356,9 @@ def _browser_context(
318356@browser_option
319357@user_agent_option
320358@reduced_motion_option
359+ @system_browser_option
360+ @browser_args_option
361+ @ignore_https_errors_option
321362def multi (
322363 config ,
323364 auth ,
@@ -328,6 +369,9 @@ def multi(
328369 browser ,
329370 user_agent ,
330371 reduced_motion ,
372+ system_browser ,
373+ browser_args ,
374+ ignore_https_errors ,
331375):
332376 """
333377 Take multiple screenshots, defined by a YAML file
@@ -358,6 +402,9 @@ def multi(
358402 user_agent = user_agent ,
359403 timeout = timeout ,
360404 reduced_motion = reduced_motion ,
405+ system_browser = system_browser ,
406+ browser_args = browser_args ,
407+ ignore_https_errors = ignore_https_errors ,
361408 )
362409 for shot in shots :
363410 if (
@@ -444,8 +491,11 @@ def accessibility(url, auth, output, javascript, timeout):
444491@browser_option
445492@user_agent_option
446493@reduced_motion_option
494+ @system_browser_option
495+ @browser_args_option
496+ @ignore_https_errors_option
447497def javascript (
448- url , javascript , input , auth , output , browser , user_agent , reduced_motion
498+ url , javascript , input , auth , output , browser , user_agent , reduced_motion , system_browser , browser_args , ignore_https_errors ,
449499):
450500 """
451501 Execute JavaScript against the page and return the result as JSON
@@ -482,6 +532,9 @@ def javascript(
482532 browser = browser ,
483533 user_agent = user_agent ,
484534 reduced_motion = reduced_motion ,
535+ system_browser = system_browser ,
536+ browser_args = browser_args ,
537+ ignore_https_errors = ignore_https_errors ,
485538 )
486539 page = context .new_page ()
487540 page .goto (url )
@@ -640,8 +693,11 @@ def install(browser):
640693)
641694@browser_option
642695@user_agent_option
696+ @system_browser_option
697+ @browser_args_option
698+ @ignore_https_errors_option
643699@click .option ("--devtools" , is_flag = True , help = "Open browser DevTools" )
644- def auth (url , context_file , browser , user_agent , devtools ):
700+ def auth (url , context_file , browser , user_agent , devtools , system_browser , browser_args , ignore_https_errors ):
645701 """
646702 Open a browser so user can manually authenticate with the specified site,
647703 then save the resulting authentication context to a file.
@@ -658,6 +714,9 @@ def auth(url, context_file, browser, user_agent, devtools):
658714 devtools = devtools ,
659715 browser = browser ,
660716 user_agent = user_agent ,
717+ system_browser = system_browser ,
718+ browser_args = browser_args ,
719+ ignore_https_errors = ignore_https_errors ,
661720 )
662721 context = browser_obj .new_context ()
663722 page = context .new_page ()
0 commit comments