10
10
import textwrap
11
11
import time
12
12
import yaml
13
+ from distutils import spawn
13
14
14
15
from shot_scraper .utils import filename_for_url , url_or_file_path
15
16
@@ -40,6 +41,25 @@ def reduced_motion_option(fn):
40
41
)(fn )
41
42
return fn
42
43
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
43
63
44
64
@click .group (
45
65
cls = DefaultGroup ,
@@ -142,6 +162,9 @@ def cli():
142
162
@browser_option
143
163
@user_agent_option
144
164
@reduced_motion_option
165
+ @system_browser_option
166
+ @browser_args_option
167
+ @ignore_https_errors_option
145
168
def shot (
146
169
url ,
147
170
auth ,
@@ -165,6 +188,9 @@ def shot(
165
188
browser ,
166
189
user_agent ,
167
190
reduced_motion ,
191
+ system_browser ,
192
+ browser_args ,
193
+ ignore_https_errors ,
168
194
):
169
195
"""
170
196
Take a single screenshot of a page or portion of a page.
@@ -224,6 +250,9 @@ def shot(
224
250
user_agent = user_agent ,
225
251
timeout = timeout ,
226
252
reduced_motion = reduced_motion ,
253
+ system_browser = system_browser ,
254
+ browser_args = browser_args ,
255
+ ignore_https_errors = ignore_https_errors ,
227
256
)
228
257
if interactive or devtools :
229
258
use_existing_page = True
@@ -267,8 +296,15 @@ def _browser_context(
267
296
user_agent = None ,
268
297
timeout = None ,
269
298
reduced_motion = False ,
299
+ system_browser = False ,
300
+ browser_args = None ,
301
+ ignore_https_errors = None ,
270
302
):
271
303
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 (' ' )
272
308
if browser == "chromium" :
273
309
browser_obj = p .chromium .launch (** browser_kwargs )
274
310
elif browser == "firefox" :
@@ -287,6 +323,8 @@ def _browser_context(
287
323
context_args ["reduced_motion" ] = "reduce"
288
324
if user_agent is not None :
289
325
context_args ["user_agent" ] = user_agent
326
+ if ignore_https_errors is not None :
327
+ context_args ["ignore_https_errors" ] = ignore_https_errors
290
328
context = browser_obj .new_context (** context_args )
291
329
if timeout :
292
330
context .set_default_timeout (timeout )
@@ -318,6 +356,9 @@ def _browser_context(
318
356
@browser_option
319
357
@user_agent_option
320
358
@reduced_motion_option
359
+ @system_browser_option
360
+ @browser_args_option
361
+ @ignore_https_errors_option
321
362
def multi (
322
363
config ,
323
364
auth ,
@@ -328,6 +369,9 @@ def multi(
328
369
browser ,
329
370
user_agent ,
330
371
reduced_motion ,
372
+ system_browser ,
373
+ browser_args ,
374
+ ignore_https_errors ,
331
375
):
332
376
"""
333
377
Take multiple screenshots, defined by a YAML file
@@ -358,6 +402,9 @@ def multi(
358
402
user_agent = user_agent ,
359
403
timeout = timeout ,
360
404
reduced_motion = reduced_motion ,
405
+ system_browser = system_browser ,
406
+ browser_args = browser_args ,
407
+ ignore_https_errors = ignore_https_errors ,
361
408
)
362
409
for shot in shots :
363
410
if (
@@ -444,8 +491,11 @@ def accessibility(url, auth, output, javascript, timeout):
444
491
@browser_option
445
492
@user_agent_option
446
493
@reduced_motion_option
494
+ @system_browser_option
495
+ @browser_args_option
496
+ @ignore_https_errors_option
447
497
def 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 ,
449
499
):
450
500
"""
451
501
Execute JavaScript against the page and return the result as JSON
@@ -482,6 +532,9 @@ def javascript(
482
532
browser = browser ,
483
533
user_agent = user_agent ,
484
534
reduced_motion = reduced_motion ,
535
+ system_browser = system_browser ,
536
+ browser_args = browser_args ,
537
+ ignore_https_errors = ignore_https_errors ,
485
538
)
486
539
page = context .new_page ()
487
540
page .goto (url )
@@ -640,8 +693,11 @@ def install(browser):
640
693
)
641
694
@browser_option
642
695
@user_agent_option
696
+ @system_browser_option
697
+ @browser_args_option
698
+ @ignore_https_errors_option
643
699
@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 ):
645
701
"""
646
702
Open a browser so user can manually authenticate with the specified site,
647
703
then save the resulting authentication context to a file.
@@ -658,6 +714,9 @@ def auth(url, context_file, browser, user_agent, devtools):
658
714
devtools = devtools ,
659
715
browser = browser ,
660
716
user_agent = user_agent ,
717
+ system_browser = system_browser ,
718
+ browser_args = browser_args ,
719
+ ignore_https_errors = ignore_https_errors ,
661
720
)
662
721
context = browser_obj .new_context ()
663
722
page = context .new_page ()
0 commit comments