7
7
from seleniumbase .config import proxy_list
8
8
from seleniumbase .core import download_helper
9
9
from seleniumbase .fixtures import constants
10
+ from seleniumbase .fixtures import page_utils
11
+
12
+
13
+ def _set_chrome_options (downloads_path , proxy_string ):
14
+ chrome_options = webdriver .ChromeOptions ()
15
+ prefs = {
16
+ "download.default_directory" : downloads_path ,
17
+ "credentials_enable_service" : False ,
18
+ "profile" : {
19
+ "password_manager_enabled" : False
20
+ }
21
+ }
22
+ chrome_options .add_experimental_option ("prefs" , prefs )
23
+ chrome_options .add_argument ("--test-type" )
24
+ chrome_options .add_argument ("--no-first-run" )
25
+ chrome_options .add_argument ("--ignore-certificate-errors" )
26
+ chrome_options .add_argument ("--allow-file-access-from-files" )
27
+ chrome_options .add_argument ("--allow-insecure-localhost" )
28
+ chrome_options .add_argument ("--allow-running-insecure-content" )
29
+ chrome_options .add_argument ("--disable-infobars" )
30
+ chrome_options .add_argument ("--disable-save-password-bubble" )
31
+ chrome_options .add_argument ("--disable-single-click-autofill" )
32
+ chrome_options .add_argument ("--disable-translate" )
33
+ chrome_options .add_argument ("--disable-web-security" )
34
+ if proxy_string :
35
+ chrome_options .add_argument ('--proxy-server=%s' % proxy_string )
36
+ if settings .START_CHROME_IN_FULL_SCREEN_MODE :
37
+ # Run Chrome in full screen mode on WINDOWS
38
+ chrome_options .add_argument ("--start-maximized" )
39
+ # Run Chrome in full screen mode on MAC/Linux
40
+ chrome_options .add_argument ("--kiosk" )
41
+ return chrome_options
10
42
11
43
12
44
def _create_firefox_profile (downloads_path , proxy_string ):
@@ -42,8 +74,8 @@ def _create_firefox_profile(downloads_path, proxy_string):
42
74
43
75
def display_proxy_warning (proxy_string ):
44
76
message = ('\n \n WARNING: Proxy String ["%s"] is NOT in the expected '
45
- '"ip_address:port" format, (OR the key does not exist '
46
- 'in proxy_list.PROXY_LIST). '
77
+ '"ip_address:port" or "server:port" format, '
78
+ '(OR the key does not exist in proxy_list.PROXY_LIST). '
47
79
'*** DEFAULTING to NOT USING a Proxy Server! ***'
48
80
% proxy_string )
49
81
warnings .simplefilter ('always' , Warning ) # See Warnings
@@ -56,10 +88,24 @@ def validate_proxy_string(proxy_string):
56
88
proxy_string = proxy_list .PROXY_LIST [proxy_string ]
57
89
if not proxy_string :
58
90
return None
59
- valid = re .match ('^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d+$' , proxy_string )
60
- if valid :
61
- proxy_string = valid .group ()
91
+ valid = False
92
+ val_ip = re .match ('^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d+$' , proxy_string )
93
+ if not val_ip :
94
+ if proxy_string .startswith ('http://' ):
95
+ proxy_string = proxy_string .split ('http://' )[1 ]
96
+ elif proxy_string .startswith ('https://' ):
97
+ proxy_string = proxy_string .split ('https://' )[1 ]
98
+ elif '://' in proxy_string :
99
+ proxy_string = proxy_string .split ('://' )[1 ]
100
+ chunks = proxy_string .split (':' )
101
+ if len (chunks ) == 2 :
102
+ if re .match ('^\d+$' , chunks [1 ]):
103
+ if page_utils .is_valid_url ('http://' + proxy_string ):
104
+ valid = True
62
105
else :
106
+ proxy_string = val_ip .group ()
107
+ valid = True
108
+ if not valid :
63
109
display_proxy_warning (proxy_string )
64
110
proxy_string = None
65
111
return proxy_string
@@ -82,33 +128,14 @@ def get_remote_driver(browser_name, headless, servername, port, proxy_string):
82
128
address = "http://%s:%s/wd/hub" % (servername , port )
83
129
84
130
if browser_name == constants .Browser .GOOGLE_CHROME :
85
- chrome_options = webdriver .ChromeOptions ()
86
- prefs = {
87
- "download.default_directory" : downloads_path ,
88
- "credentials_enable_service" : False ,
89
- "profile" : {
90
- "password_manager_enabled" : False
91
- }
92
- }
93
- chrome_options .add_experimental_option ("prefs" , prefs )
94
- chrome_options .add_argument ("--allow-file-access-from-files" )
95
- chrome_options .add_argument ("--allow-running-insecure-content" )
96
- chrome_options .add_argument ("--disable-infobars" )
131
+ chrome_options = _set_chrome_options (downloads_path , proxy_string )
97
132
if headless :
98
133
chrome_options .add_argument ("--headless" )
99
- if proxy_string :
100
- chrome_options .add_argument ('--proxy-server=%s' % proxy_string )
101
- if settings .START_CHROME_IN_FULL_SCREEN_MODE :
102
- # Run Chrome in full screen mode on WINDOWS
103
- chrome_options .add_argument ("--start-maximized" )
104
- # Run Chrome in full screen mode on MAC/Linux
105
- chrome_options .add_argument ("--kiosk" )
106
134
capabilities = chrome_options .to_capabilities ()
107
135
return webdriver .Remote (
108
136
command_executor = address ,
109
137
desired_capabilities = capabilities )
110
-
111
- if browser_name == constants .Browser .FIREFOX :
138
+ elif browser_name == constants .Browser .FIREFOX :
112
139
try :
113
140
# Use Geckodriver for Firefox if it's on the PATH
114
141
profile = _create_firefox_profile (downloads_path , proxy_string )
@@ -136,23 +163,22 @@ def get_remote_driver(browser_name, headless, servername, port, proxy_string):
136
163
command_executor = address ,
137
164
desired_capabilities = capabilities ,
138
165
browser_profile = profile )
139
-
140
- if browser_name == constants .Browser .INTERNET_EXPLORER :
166
+ elif browser_name == constants .Browser .INTERNET_EXPLORER :
141
167
return webdriver .Remote (
142
168
command_executor = address ,
143
169
desired_capabilities = (
144
170
webdriver .DesiredCapabilities .INTERNETEXPLORER ))
145
- if browser_name == constants .Browser .EDGE :
171
+ elif browser_name == constants .Browser .EDGE :
146
172
return webdriver .Remote (
147
173
command_executor = address ,
148
174
desired_capabilities = (
149
175
webdriver .DesiredCapabilities .EDGE ))
150
- if browser_name == constants .Browser .SAFARI :
176
+ elif browser_name == constants .Browser .SAFARI :
151
177
return webdriver .Remote (
152
178
command_executor = address ,
153
179
desired_capabilities = (
154
180
webdriver .DesiredCapabilities .SAFARI ))
155
- if browser_name == constants .Browser .PHANTOM_JS :
181
+ elif browser_name == constants .Browser .PHANTOM_JS :
156
182
with warnings .catch_warnings ():
157
183
# Ignore "PhantomJS has been deprecated" UserWarning
158
184
warnings .simplefilter ("ignore" , category = UserWarning )
@@ -195,40 +221,22 @@ def get_local_driver(browser_name, headless, proxy_string):
195
221
if headless :
196
222
raise Exception (e )
197
223
return webdriver .Firefox ()
198
- if browser_name == constants .Browser .INTERNET_EXPLORER :
224
+ elif browser_name == constants .Browser .INTERNET_EXPLORER :
199
225
return webdriver .Ie ()
200
- if browser_name == constants .Browser .EDGE :
226
+ elif browser_name == constants .Browser .EDGE :
201
227
return webdriver .Edge ()
202
- if browser_name == constants .Browser .SAFARI :
228
+ elif browser_name == constants .Browser .SAFARI :
203
229
return webdriver .Safari ()
204
- if browser_name == constants .Browser .PHANTOM_JS :
230
+ elif browser_name == constants .Browser .PHANTOM_JS :
205
231
with warnings .catch_warnings ():
206
232
# Ignore "PhantomJS has been deprecated" UserWarning
207
233
warnings .simplefilter ("ignore" , category = UserWarning )
208
234
return webdriver .PhantomJS ()
209
- if browser_name == constants .Browser .GOOGLE_CHROME :
235
+ elif browser_name == constants .Browser .GOOGLE_CHROME :
210
236
try :
211
- chrome_options = webdriver .ChromeOptions ()
212
- prefs = {
213
- "download.default_directory" : downloads_path ,
214
- "credentials_enable_service" : False ,
215
- "profile" : {
216
- "password_manager_enabled" : False
217
- }
218
- }
219
- chrome_options .add_experimental_option ("prefs" , prefs )
220
- chrome_options .add_argument ("--allow-file-access-from-files" )
221
- chrome_options .add_argument ("--allow-running-insecure-content" )
222
- chrome_options .add_argument ("--disable-infobars" )
237
+ chrome_options = _set_chrome_options (downloads_path , proxy_string )
223
238
if headless :
224
239
chrome_options .add_argument ("--headless" )
225
- if proxy_string :
226
- chrome_options .add_argument ('--proxy-server=%s' % proxy_string )
227
- if settings .START_CHROME_IN_FULL_SCREEN_MODE :
228
- # Run Chrome in full screen mode on WINDOWS
229
- chrome_options .add_argument ("--start-maximized" )
230
- # Run Chrome in full screen mode on MAC/Linux
231
- chrome_options .add_argument ("--kiosk" )
232
240
return webdriver .Chrome (options = chrome_options )
233
241
except Exception as e :
234
242
if headless :
0 commit comments