1
- import json
2
1
import logging
3
2
import os
4
- import random
5
3
import re
6
4
import sys
7
5
import time
8
6
import urllib3
9
7
import warnings
10
8
from selenium import webdriver
11
- from selenium .common .exceptions import WebDriverException
12
- from selenium .webdriver .common .desired_capabilities import DesiredCapabilities
13
9
from seleniumbase .config import proxy_list
14
10
from seleniumbase .config import settings
15
11
from seleniumbase .core import download_helper
16
12
from seleniumbase .core import proxy_helper
17
13
from seleniumbase .fixtures import constants
18
- from seleniumbase .fixtures import page_utils
19
14
from seleniumbase import drivers # webdriver storage folder for SeleniumBase
20
15
from seleniumbase import extensions # browser extensions storage folder
21
16
urllib3 .disable_warnings ()
@@ -98,6 +93,7 @@ def _add_chrome_proxy_extension(
98
93
""" Implementation of https://stackoverflow.com/a/35293284 for
99
94
https://stackoverflow.com/questions/12848327/
100
95
(Run Selenium on a proxy server that requires authentication.) """
96
+ import random
101
97
arg_join = " " .join (sys .argv )
102
98
if not ("-n" in sys .argv or "-n=" in arg_join or arg_join == "-c" ):
103
99
# Single-threaded
@@ -267,7 +263,7 @@ def _set_chrome_options(
267
263
if proxy_auth :
268
264
chrome_options = _add_chrome_proxy_extension (
269
265
chrome_options , proxy_string , proxy_user , proxy_pass )
270
- chrome_options .add_argument (' --proxy-server=%s' % proxy_string )
266
+ chrome_options .add_argument (" --proxy-server=%s" % proxy_string )
271
267
if headless :
272
268
if not proxy_auth and not browser_name == constants .Browser .OPERA :
273
269
# Headless Chrome doesn't support extensions, which are
@@ -291,7 +287,7 @@ def _set_chrome_options(
291
287
# To access the Remote Debugger, go to: http://localhost:9222
292
288
# while a Chromium driver is running.
293
289
# Info: https://chromedevtools.github.io/devtools-protocol/
294
- chrome_options .add_argument (' --remote-debugging-port=9222' )
290
+ chrome_options .add_argument (" --remote-debugging-port=9222" )
295
291
if swiftshader :
296
292
chrome_options .add_argument ("--use-gl=swiftshader" )
297
293
else :
@@ -320,37 +316,38 @@ def _set_safari_capabilities():
320
316
return safari_capabilities
321
317
322
318
323
- def _create_firefox_profile (
324
- downloads_path , locale_code , proxy_string , user_agent , disable_csp ):
325
- profile = webdriver .FirefoxProfile ()
326
- profile .accept_untrusted_certs = True
327
- profile .set_preference ("reader.parse-on-load.enabled" , False )
328
- profile .set_preference ("pdfjs.disabled" , True )
329
- profile .set_preference ("app.update.auto" , False )
330
- profile .set_preference ("app.update.enabled" , False )
331
- profile .set_preference ("app.update.silent" , True )
332
- profile .set_preference ("browser.formfill.enable" , False )
333
- profile .set_preference ("browser.privatebrowsing.autostart" , True )
334
- profile .set_preference ("devtools.errorconsole.enabled" , True )
335
- profile .set_preference ("dom.webnotifications.enabled" , False )
336
- profile .set_preference ("dom.disable_beforeunload" , True )
337
- profile .set_preference ("browser.contentblocking.database.enabled" , False )
338
- profile .set_preference ("extensions.allowPrivateBrowsingByDefault" , True )
339
- profile .set_preference ("extensions.PrivateBrowsing.notification" , False )
340
- profile .set_preference ("extensions.systemAddon.update.enabled" , False )
341
- profile .set_preference ("extensions.update.autoUpdateDefault" , False )
342
- profile .set_preference ("extensions.update.enabled" , False )
343
- profile .set_preference ("extensions.update.silent" , True )
344
- profile .set_preference (
319
+ def _set_firefox_options (
320
+ downloads_path , headless , locale_code ,
321
+ proxy_string , user_agent , disable_csp ):
322
+ options = webdriver .FirefoxOptions ()
323
+ options .accept_untrusted_certs = True
324
+ options .set_preference ("reader.parse-on-load.enabled" , False )
325
+ options .set_preference ("pdfjs.disabled" , True )
326
+ options .set_preference ("app.update.auto" , False )
327
+ options .set_preference ("app.update.enabled" , False )
328
+ options .set_preference ("app.update.silent" , True )
329
+ options .set_preference ("browser.formfill.enable" , False )
330
+ options .set_preference ("browser.privatebrowsing.autostart" , True )
331
+ options .set_preference ("devtools.errorconsole.enabled" , True )
332
+ options .set_preference ("dom.webnotifications.enabled" , False )
333
+ options .set_preference ("dom.disable_beforeunload" , True )
334
+ options .set_preference ("browser.contentblocking.database.enabled" , False )
335
+ options .set_preference ("extensions.allowPrivateBrowsingByDefault" , True )
336
+ options .set_preference ("extensions.PrivateBrowsing.notification" , False )
337
+ options .set_preference ("extensions.systemAddon.update.enabled" , False )
338
+ options .set_preference ("extensions.update.autoUpdateDefault" , False )
339
+ options .set_preference ("extensions.update.enabled" , False )
340
+ options .set_preference ("extensions.update.silent" , True )
341
+ options .set_preference (
345
342
"datareporting.healthreport.logging.consoleEnabled" , False )
346
- profile .set_preference ("datareporting.healthreport.service.enabled" , False )
347
- profile .set_preference (
343
+ options .set_preference ("datareporting.healthreport.service.enabled" , False )
344
+ options .set_preference (
348
345
"datareporting.healthreport.service.firstRun" , False )
349
- profile .set_preference ("datareporting.healthreport.uploadEnabled" , False )
350
- profile .set_preference ("datareporting.policy.dataSubmissionEnabled" , False )
351
- profile .set_preference (
346
+ options .set_preference ("datareporting.healthreport.uploadEnabled" , False )
347
+ options .set_preference ("datareporting.policy.dataSubmissionEnabled" , False )
348
+ options .set_preference (
352
349
"datareporting.policy.dataSubmissionPolicyAccepted" , False )
353
- profile .set_preference ("toolkit.telemetry.unified" , False )
350
+ options .set_preference ("toolkit.telemetry.unified" , False )
354
351
if proxy_string :
355
352
socks_proxy = False
356
353
socks_ver = 0
@@ -366,44 +363,46 @@ def _create_firefox_profile(
366
363
else :
367
364
proxy_server = proxy_string .split (':' )[0 ]
368
365
proxy_port = proxy_string .split (':' )[1 ]
369
- profile .set_preference ("network.proxy.type" , 1 )
366
+ options .set_preference ("network.proxy.type" , 1 )
370
367
if socks_proxy :
371
- profile .set_preference ('network.proxy.socks' , proxy_server )
372
- profile .set_preference ('network.proxy.socks_port' , int (proxy_port ))
373
- profile .set_preference ('network.proxy.socks_version' , socks_ver )
368
+ options .set_preference ('network.proxy.socks' , proxy_server )
369
+ options .set_preference ('network.proxy.socks_port' , int (proxy_port ))
370
+ options .set_preference ('network.proxy.socks_version' , socks_ver )
374
371
else :
375
- profile .set_preference ("network.proxy.http" , proxy_server )
376
- profile .set_preference ("network.proxy.http_port" , int (proxy_port ))
377
- profile .set_preference ("network.proxy.ssl" , proxy_server )
378
- profile .set_preference ("network.proxy.ssl_port" , int (proxy_port ))
372
+ options .set_preference ("network.proxy.http" , proxy_server )
373
+ options .set_preference ("network.proxy.http_port" , int (proxy_port ))
374
+ options .set_preference ("network.proxy.ssl" , proxy_server )
375
+ options .set_preference ("network.proxy.ssl_port" , int (proxy_port ))
379
376
if user_agent :
380
- profile .set_preference ("general.useragent.override" , user_agent )
381
- profile .set_preference (
377
+ options .set_preference ("general.useragent.override" , user_agent )
378
+ options .set_preference (
382
379
"security.mixed_content.block_active_content" , False )
383
380
if settings .DISABLE_CSP_ON_FIREFOX or disable_csp :
384
- profile .set_preference ("security.csp.enable" , False )
385
- profile .set_preference (
381
+ options .set_preference ("security.csp.enable" , False )
382
+ options .set_preference (
386
383
"browser.download.manager.showAlertOnComplete" , False )
384
+ if headless :
385
+ options .add_argument ("--headless" )
387
386
if locale_code :
388
- profile .set_preference ("intl.accept_languages" , locale_code )
389
- profile .set_preference ("browser.shell.checkDefaultBrowser" , False )
390
- profile .set_preference ("browser.startup.page" , 0 )
391
- profile .set_preference ("browser.download.panel.shown" , False )
392
- profile .set_preference (
387
+ options .set_preference ("intl.accept_languages" , locale_code )
388
+ options .set_preference ("browser.shell.checkDefaultBrowser" , False )
389
+ options .set_preference ("browser.startup.page" , 0 )
390
+ options .set_preference ("browser.download.panel.shown" , False )
391
+ options .set_preference (
393
392
"browser.download.animateNotifications" , False )
394
- profile .set_preference ("browser.download.dir" , downloads_path )
395
- profile .set_preference ("browser.download.folderList" , 2 )
396
- profile .set_preference ("browser.helperApps.alwaysAsk.force" , False )
397
- profile .set_preference (
393
+ options .set_preference ("browser.download.dir" , downloads_path )
394
+ options .set_preference ("browser.download.folderList" , 2 )
395
+ options .set_preference ("browser.helperApps.alwaysAsk.force" , False )
396
+ options .set_preference (
398
397
"browser.download.manager.showWhenStarting" , False )
399
- profile .set_preference (
398
+ options .set_preference (
400
399
"browser.helperApps.neverAsk.saveToDisk" ,
401
400
("application/pdf, application/zip, application/octet-stream, "
402
401
"text/csv, text/xml, application/xml, text/plain, "
403
402
"text/octet-stream, application/x-gzip, application/x-tar "
404
403
"application/"
405
404
"vnd.openxmlformats-officedocument.spreadsheetml.sheet" ))
406
- return profile
405
+ return options
407
406
408
407
409
408
def display_proxy_warning (proxy_string ):
@@ -422,6 +421,7 @@ def display_proxy_warning(proxy_string):
422
421
423
422
424
423
def validate_proxy_string (proxy_string ):
424
+ from seleniumbase .fixtures import page_utils
425
425
if proxy_string in proxy_list .PROXY_LIST .keys ():
426
426
proxy_string = proxy_list .PROXY_LIST [proxy_string ]
427
427
if not proxy_string :
@@ -464,7 +464,7 @@ def validate_proxy_string(proxy_string):
464
464
465
465
466
466
def get_driver (browser_name , headless = False , locale_code = None ,
467
- use_grid = False , servername = ' localhost' , port = 4444 ,
467
+ use_grid = False , servername = " localhost" , port = 4444 ,
468
468
proxy_string = None , user_agent = None ,
469
469
cap_file = None , cap_string = None ,
470
470
disable_csp = None , enable_ws = None , enable_sync = None ,
@@ -540,6 +540,7 @@ def get_remote_driver(
540
540
from seleniumbase .core import capabilities_parser
541
541
desired_caps = capabilities_parser .get_desired_capabilities (cap_file )
542
542
if cap_string :
543
+ import json
543
544
try :
544
545
extra_caps = json .loads (cap_string )
545
546
except Exception as e :
@@ -568,52 +569,28 @@ def get_remote_driver(
568
569
capabilities = chrome_options .to_capabilities ()
569
570
for key in desired_caps .keys ():
570
571
capabilities [key ] = desired_caps [key ]
572
+ warnings .simplefilter ("ignore" , category = DeprecationWarning )
571
573
return webdriver .Remote (
572
574
command_executor = address ,
573
575
desired_capabilities = capabilities ,
574
576
keep_alive = True )
575
577
elif browser_name == constants .Browser .FIREFOX :
576
- try :
577
- # Use Geckodriver for Firefox if it's on the PATH
578
- profile = _create_firefox_profile (
579
- downloads_path , locale_code ,
580
- proxy_string , user_agent , disable_csp )
581
- firefox_capabilities = DesiredCapabilities .FIREFOX .copy ()
582
- firefox_capabilities ['marionette' ] = True
583
- if headless :
584
- firefox_capabilities ['moz:firefoxOptions' ] = (
585
- {'args' : ['-headless' ]})
586
- for key in desired_caps .keys ():
587
- firefox_capabilities [key ] = desired_caps [key ]
588
- capabilities = firefox_capabilities
589
- warnings .simplefilter ("ignore" , category = DeprecationWarning )
590
- return webdriver .Remote (
591
- command_executor = address ,
592
- desired_capabilities = capabilities ,
593
- browser_profile = profile ,
594
- keep_alive = True )
595
- except WebDriverException :
596
- # Don't use Geckodriver: Only works for old versions of Firefox
597
- profile = _create_firefox_profile (
598
- downloads_path , locale_code ,
599
- proxy_string , user_agent , disable_csp )
600
- firefox_capabilities = DesiredCapabilities .FIREFOX .copy ()
601
- firefox_capabilities ['marionette' ] = False
602
- if headless :
603
- firefox_capabilities ['moz:firefoxOptions' ] = (
604
- {'args' : ['-headless' ]})
605
- for key in desired_caps .keys ():
606
- firefox_capabilities [key ] = desired_caps [key ]
607
- capabilities = firefox_capabilities
608
- return webdriver .Remote (
609
- command_executor = address ,
610
- desired_capabilities = capabilities ,
611
- browser_profile = profile ,
612
- keep_alive = True )
578
+ firefox_options = _set_firefox_options (
579
+ downloads_path , headless , locale_code ,
580
+ proxy_string , user_agent , disable_csp )
581
+ capabilities = firefox_options .to_capabilities ()
582
+ for key in desired_caps .keys ():
583
+ capabilities [key ] = desired_caps [key ]
584
+ warnings .simplefilter ("ignore" , category = DeprecationWarning )
585
+ return webdriver .Remote (
586
+ command_executor = address ,
587
+ desired_capabilities = capabilities ,
588
+ keep_alive = True )
613
589
elif browser_name == constants .Browser .INTERNET_EXPLORER :
614
590
capabilities = webdriver .DesiredCapabilities .INTERNETEXPLORER
615
591
for key in desired_caps .keys ():
616
592
capabilities [key ] = desired_caps [key ]
593
+ warnings .simplefilter ("ignore" , category = DeprecationWarning )
617
594
return webdriver .Remote (
618
595
command_executor = address ,
619
596
desired_capabilities = capabilities ,
@@ -622,6 +599,7 @@ def get_remote_driver(
622
599
capabilities = webdriver .DesiredCapabilities .EDGE
623
600
for key in desired_caps .keys ():
624
601
capabilities [key ] = desired_caps [key ]
602
+ warnings .simplefilter ("ignore" , category = DeprecationWarning )
625
603
return webdriver .Remote (
626
604
command_executor = address ,
627
605
desired_capabilities = capabilities ,
@@ -630,6 +608,7 @@ def get_remote_driver(
630
608
capabilities = webdriver .DesiredCapabilities .SAFARI
631
609
for key in desired_caps .keys ():
632
610
capabilities [key ] = desired_caps [key ]
611
+ warnings .simplefilter ("ignore" , category = DeprecationWarning )
633
612
return webdriver .Remote (
634
613
command_executor = address ,
635
614
desired_capabilities = capabilities ,
@@ -638,6 +617,7 @@ def get_remote_driver(
638
617
capabilities = webdriver .DesiredCapabilities .OPERA
639
618
for key in desired_caps .keys ():
640
619
capabilities [key ] = desired_caps [key ]
620
+ warnings .simplefilter ("ignore" , category = DeprecationWarning )
641
621
return webdriver .Remote (
642
622
command_executor = address ,
643
623
desired_capabilities = capabilities ,
@@ -699,56 +679,31 @@ def get_local_driver(
699
679
downloads_path = download_helper .get_downloads_folder ()
700
680
701
681
if browser_name == constants .Browser .FIREFOX :
702
- try :
682
+ firefox_options = _set_firefox_options (
683
+ downloads_path , headless , locale_code ,
684
+ proxy_string , user_agent , disable_csp )
685
+ if LOCAL_GECKODRIVER and os .path .exists (LOCAL_GECKODRIVER ):
703
686
try :
704
- # Use Geckodriver for Firefox if it's on the PATH
705
- profile = _create_firefox_profile (
706
- downloads_path , locale_code ,
707
- proxy_string , user_agent , disable_csp )
708
- firefox_capabilities = DesiredCapabilities .FIREFOX .copy ()
709
- firefox_capabilities ['marionette' ] = True
710
- options = webdriver .FirefoxOptions ()
711
- if headless :
712
- options .add_argument ('-headless' )
713
- firefox_capabilities ['moz:firefoxOptions' ] = (
714
- {'args' : ['-headless' ]})
715
- if LOCAL_GECKODRIVER and os .path .exists (LOCAL_GECKODRIVER ):
716
- try :
717
- make_driver_executable_if_not (LOCAL_GECKODRIVER )
718
- except Exception as e :
719
- logging .debug ("\n Warning: Could not make geckodriver"
720
- " executable: %s" % e )
721
- elif not is_geckodriver_on_path ():
722
- args = " " .join (sys .argv )
723
- if not ("-n" in sys .argv or "-n=" in args or args == "-c" ):
724
- # (Not multithreaded)
725
- from seleniumbase .console_scripts import sb_install
726
- sys_args = sys .argv # Save a copy of current sys args
727
- print ("\n Warning: geckodriver not found!"
728
- " Installing now:" )
729
- try :
730
- sb_install .main (override = "geckodriver" )
731
- except Exception as e :
732
- print ("\n Warning: Could not install geckodriver: "
733
- "%s" % e )
734
- sys .argv = sys_args # Put back the original sys args
735
- firefox_driver = webdriver .Firefox (
736
- firefox_profile = profile ,
737
- capabilities = firefox_capabilities ,
738
- options = options )
739
- except Exception :
740
- profile = _create_firefox_profile (
741
- downloads_path , locale_code ,
742
- proxy_string , user_agent , disable_csp )
743
- firefox_capabilities = DesiredCapabilities .FIREFOX .copy ()
744
- firefox_driver = webdriver .Firefox (
745
- firefox_profile = profile ,
746
- capabilities = firefox_capabilities )
747
- return firefox_driver
748
- except Exception as e :
749
- if headless :
750
- raise Exception (e )
751
- return webdriver .Firefox ()
687
+ make_driver_executable_if_not (LOCAL_GECKODRIVER )
688
+ except Exception as e :
689
+ logging .debug ("\n Warning: Could not make geckodriver"
690
+ " executable: %s" % e )
691
+ elif not is_geckodriver_on_path ():
692
+ args = " " .join (sys .argv )
693
+ if not ("-n" in sys .argv or "-n=" in args or args == "-c" ):
694
+ # (Not multithreaded)
695
+ from seleniumbase .console_scripts import sb_install
696
+ sys_args = sys .argv # Save a copy of current sys args
697
+ print ("\n Warning: geckodriver not found!"
698
+ " Installing now:" )
699
+ try :
700
+ sb_install .main (override = "geckodriver" )
701
+ except Exception as e :
702
+ print ("\n Warning: Could not install geckodriver: "
703
+ "%s" % e )
704
+ sys .argv = sys_args # Put back the original sys args
705
+ warnings .simplefilter ("ignore" , category = DeprecationWarning )
706
+ return webdriver .Firefox (options = firefox_options )
752
707
elif browser_name == constants .Browser .INTERNET_EXPLORER :
753
708
if not IS_WINDOWS :
754
709
raise Exception (
@@ -898,7 +853,7 @@ def get_local_driver(
898
853
if proxy_auth :
899
854
edge_options = _add_chrome_proxy_extension (
900
855
edge_options , proxy_string , proxy_user , proxy_pass )
901
- edge_options .add_argument (' --proxy-server=%s' % proxy_string )
856
+ edge_options .add_argument (" --proxy-server=%s" % proxy_string )
902
857
edge_options .add_argument ("--test-type" )
903
858
edge_options .add_argument ("--log-level=3" )
904
859
edge_options .add_argument ("--no-first-run" )
@@ -915,7 +870,7 @@ def get_local_driver(
915
870
# To access the Remote Debugger, go to: http://localhost:9222
916
871
# while a Chromium driver is running.
917
872
# Info: https://chromedevtools.github.io/devtools-protocol/
918
- edge_options .add_argument (' --remote-debugging-port=9222' )
873
+ edge_options .add_argument (" --remote-debugging-port=9222" )
919
874
if swiftshader :
920
875
edge_options .add_argument ("--use-gl=swiftshader" )
921
876
else :
0 commit comments