4
4
from selenium import webdriver
5
5
6
6
import settings
7
+ from settings import DESIRED_CAP
7
8
8
9
9
10
def launch_driver (driver_name = settings .DRIVER , desired_capabilities = None ):
@@ -19,57 +20,99 @@ def launch_driver(driver_name=settings.DRIVER, desired_capabilities=None):
19
20
driver_cls = getattr (webdriver , settings .DRIVER )
20
21
21
22
if driver_name == 'Remote' :
23
+
22
24
if desired_capabilities is None :
23
25
desired_capabilities = settings .DESIRED_CAP
24
26
command_executor = 'http://{}:{}@hub.browserstack.com:80/wd/hub' .format (
25
27
settings .BSTACK_USER , settings .BSTACK_KEY
26
28
)
27
29
28
- # Create a temporary Firefox WebDriver to fetch the current user agent
29
- temp_options = webdriver .FirefoxOptions ()
30
- temp_driver = webdriver .Firefox (options = temp_options )
31
- default_user_agent = temp_driver .execute_script ('return navigator.userAgent;' )
32
- temp_driver .quit ()
33
-
34
- # Append "Selenium Bot" to the existing user agent
35
- custom_user_agent = f'{ default_user_agent } Selenium Bot'
36
-
37
- # NOTE: BrowserStack does support the use of Chrome Options, but we are not
38
- # currently using any of them. Below are several steps to setup preferences
39
- # that are specific to Firefox. Currently when running Chrome or Edge in
40
- # BrowserStack we are running with the default base install options.
41
-
42
- from selenium .webdriver .firefox .options import Options
43
-
44
- ffo = Options ()
45
-
46
- # Set custom user agent
47
- ffo .set_preference ('general.useragent.override' , custom_user_agent )
48
-
49
- # Set the default download location [0=Desktop, 1=Downloads, 2=Specified location]
50
- ffo .set_preference ('browser.download.folderList' , 1 )
51
-
52
- # Disable the OS-level pop-up modal
53
- ffo .set_preference ('browser.download.manager.showWhenStarting' , False )
54
- ffo .set_preference ('browser.helperApps.alwaysAsk.force' , False )
55
- ffo .set_preference ('browser.download.manager.alertOnEXEOpen' , False )
56
- ffo .set_preference ('browser.download.manager.closeWhenDone' , True )
57
- ffo .set_preference ('browser.download.manager.showAlertOnComplete' , False )
58
- ffo .set_preference ('browser.download.manager.useWindow' , False )
59
- # Specify the file types supported by the download
60
- ffo .set_preference (
61
- 'browser.helperApps.neverAsk.saveToDisk' ,
62
- 'text/plain, application/octet-stream, application/binary, text/csv, application/csv, '
63
- 'application/excel, text/comma-separated-values, text/xml, application/xml, binary/octet-stream' ,
64
- )
65
- # Block Third Party Tracking Cookies (Default in Firefox is now 5 which blocks
66
- # all Cross-site cookies)
67
- ffo .set_preference ('network.cookie.cookieBehavior' , 4 )
68
- driver = driver_cls (
69
- command_executor = command_executor ,
70
- desired_capabilities = desired_capabilities ,
71
- options = ffo ,
72
- )
30
+ if settings .BUILD == 'firefox' :
31
+ # Create a temporary Firefox WebDriver to fetch the current user agent
32
+ temp_options = webdriver .FirefoxOptions ()
33
+ temp_driver = webdriver .Firefox (options = temp_options )
34
+ default_user_agent = temp_driver .execute_script (
35
+ 'return navigator.userAgent;'
36
+ )
37
+ temp_driver .quit ()
38
+
39
+ # Append "Selenium Bot" to the existing user agent
40
+ custom_user_agent = f'{ default_user_agent } OSF Selenium Bot'
41
+
42
+ # NOTE: BrowserStack does support the use of Chrome Options, but we are not
43
+ # currently using any of them. Below are several steps to setup preferences
44
+ # that are specific to Firefox. Currently when running Chrome or Edge in
45
+ # BrowserStack we are running with the default base install options.
46
+
47
+ from selenium .webdriver .firefox .options import Options
48
+
49
+ ffo = Options ()
50
+
51
+ # Set custom user agent
52
+ ffo .set_preference ('general.useragent.override' , custom_user_agent )
53
+
54
+ # Set the default download location [0=Desktop, 1=Downloads, 2=Specified location]
55
+ ffo .set_preference ('browser.download.folderList' , 1 )
56
+
57
+ # Disable the OS-level pop-up modal
58
+ ffo .set_preference ('browser.download.manager.showWhenStarting' , False )
59
+ ffo .set_preference ('browser.helperApps.alwaysAsk.force' , False )
60
+ ffo .set_preference ('browser.download.manager.alertOnEXEOpen' , False )
61
+ ffo .set_preference ('browser.download.manager.closeWhenDone' , True )
62
+ ffo .set_preference ('browser.download.manager.showAlertOnComplete' , False )
63
+ ffo .set_preference ('browser.download.manager.useWindow' , False )
64
+ # Specify the file types supported by the download
65
+ ffo .set_preference (
66
+ 'browser.helperApps.neverAsk.saveToDisk' ,
67
+ 'text/plain, application/octet-stream, application/binary, text/csv, application/csv, '
68
+ 'application/excel, text/comma-separated-values, text/xml, application/xml, binary/octet-stream' ,
69
+ )
70
+ # Block Third Party Tracking Cookies (Default in Firefox is now 5 which blocks
71
+ # all Cross-site cookies)
72
+ ffo .set_preference ('network.cookie.cookieBehavior' , 4 )
73
+ driver = driver_cls (
74
+ command_executor = command_executor ,
75
+ desired_capabilities = desired_capabilities ,
76
+ options = ffo ,
77
+ )
78
+ elif settings .BUILD == 'chrome' :
79
+ from selenium .webdriver .chrome .options import Options
80
+
81
+ chrome_options : Options = Options ()
82
+ chrome_options .add_argument ('--disable-gpu' )
83
+ chrome_options .add_argument ('window-size=1200x600' )
84
+
85
+ # Fetch default user agent
86
+ temp_driver = driver_cls (
87
+ command_executor = command_executor ,
88
+ desired_capabilities = desired_capabilities ,
89
+ options = chrome_options ,
90
+ )
91
+ default_user_agent = temp_driver .execute_script (
92
+ 'return navigator.userAgent;'
93
+ )
94
+ temp_driver .quit ()
95
+
96
+ # Append "OSF Selenium Bot" to the existing user agent
97
+ custom_user_agent = f'{ default_user_agent } OSF Selenium Bot'
98
+ chrome_options .add_argument (f'user-agent={ custom_user_agent } ' )
99
+
100
+ # Make a copy of desired capabilities for Chrome
101
+ desired_capabilities = DESIRED_CAP .copy ()
102
+
103
+ # Attach Chrome options
104
+ desired_capabilities ['goog:chromeOptions' ] = {
105
+ 'args' : chrome_options .arguments
106
+ }
107
+
108
+ driver = driver_cls (
109
+ command_executor = command_executor ,
110
+ desired_capabilities = desired_capabilities ,
111
+ options = chrome_options ,
112
+ )
113
+
114
+ print (driver .execute_script (DESIRED_CAP ))
115
+ print (driver .execute_script ('return navigator.userAgent;' ))
73
116
74
117
elif driver_name == 'Chrome' and settings .HEADLESS :
75
118
from selenium .webdriver .chrome .options import Options
0 commit comments