1
+ import os
1
2
import re
3
+ import sys
2
4
import warnings
3
5
from selenium import webdriver
4
6
from selenium .common .exceptions import WebDriverException
8
10
from seleniumbase .core import download_helper
9
11
from seleniumbase .fixtures import constants
10
12
from seleniumbase .fixtures import page_utils
13
+ import drivers # webdriver storage folder for SeleniumBase
14
+ DRIVER_DIR = os .path .dirname (os .path .realpath (drivers .__file__ ))
15
+ LOCAL_CHROMEDRIVER = None
16
+ LOCAL_GECKODRIVER = None
17
+ LOCAL_EDGEDRIVER = None
18
+ if "darwin" in sys .platform or "linux" in sys .platform :
19
+ LOCAL_CHROMEDRIVER = DRIVER_DIR + '/chromedriver'
20
+ LOCAL_GECKODRIVER = DRIVER_DIR + '/geckodriver'
21
+ elif "win32" in sys .platform or "win64" in sys .platform :
22
+ LOCAL_EDGEDRIVER = DRIVER_DIR + '/MicrosoftWebDriver.exe'
23
+ LOCAL_CHROMEDRIVER = DRIVER_DIR + '/chromedriver.exe'
24
+ LOCAL_GECKODRIVER = DRIVER_DIR + '/geckodriver.exe'
25
+ else :
26
+ # Cannot determine system
27
+ pass # SeleniumBase will use web drivers from the System PATH by default
28
+
29
+
30
+ def make_executable (file_path ):
31
+ # Set permissions to: "If you can read it, you can execute it."
32
+ mode = os .stat (file_path ).st_mode
33
+ mode |= (mode & 0o444 ) >> 2 # copy R bits to X
34
+ os .chmod (file_path , mode )
35
+
36
+
37
+ def make_driver_executable_if_not (driver_path ):
38
+ # Verify driver has executable permissions. If not, add them.
39
+ permissions = oct (os .stat (driver_path )[0 ])[- 3 :]
40
+ if '4' in permissions or '6' in permissions :
41
+ # We want at least a '5' or '7' to make sure it's executable
42
+ make_executable (driver_path )
11
43
12
44
13
45
def _set_chrome_options (downloads_path , proxy_string ):
@@ -132,6 +164,8 @@ def get_remote_driver(browser_name, headless, servername, port, proxy_string):
132
164
chrome_options = _set_chrome_options (downloads_path , proxy_string )
133
165
if headless :
134
166
chrome_options .add_argument ("--headless" )
167
+ chrome_options .add_argument ("--disable-gpu" )
168
+ chrome_options .add_argument ("--no-sandbox" )
135
169
capabilities = chrome_options .to_capabilities ()
136
170
return webdriver .Remote (
137
171
command_executor = address ,
@@ -207,9 +241,18 @@ def get_local_driver(browser_name, headless, proxy_string):
207
241
options = webdriver .FirefoxOptions ()
208
242
if headless :
209
243
options .add_argument ('-headless' )
210
- firefox_driver = webdriver .Firefox (
211
- firefox_profile = profile , capabilities = firefox_capabilities ,
212
- firefox_options = options )
244
+ if LOCAL_GECKODRIVER and os .path .exists (LOCAL_GECKODRIVER ):
245
+ make_driver_executable_if_not (LOCAL_GECKODRIVER )
246
+ firefox_driver = webdriver .Firefox (
247
+ firefox_profile = profile ,
248
+ capabilities = firefox_capabilities ,
249
+ firefox_options = options ,
250
+ executable_path = LOCAL_GECKODRIVER )
251
+ else :
252
+ firefox_driver = webdriver .Firefox (
253
+ firefox_profile = profile ,
254
+ capabilities = firefox_capabilities ,
255
+ firefox_options = options )
213
256
except WebDriverException :
214
257
# Don't use Geckodriver: Only works for old versions of Firefox
215
258
profile = _create_firefox_profile (downloads_path , proxy_string )
@@ -225,7 +268,14 @@ def get_local_driver(browser_name, headless, proxy_string):
225
268
elif browser_name == constants .Browser .INTERNET_EXPLORER :
226
269
return webdriver .Ie ()
227
270
elif browser_name == constants .Browser .EDGE :
228
- return webdriver .Edge ()
271
+ edge_capabilities = DesiredCapabilities .EDGE .copy ()
272
+ if LOCAL_EDGEDRIVER and os .path .exists (LOCAL_EDGEDRIVER ):
273
+ make_driver_executable_if_not (LOCAL_EDGEDRIVER )
274
+ return webdriver .Edge (
275
+ capabilities = edge_capabilities ,
276
+ executable_path = LOCAL_EDGEDRIVER )
277
+ else :
278
+ return webdriver .Edge (capabilities = edge_capabilities )
229
279
elif browser_name == constants .Browser .SAFARI :
230
280
return webdriver .Safari ()
231
281
elif browser_name == constants .Browser .PHANTOM_JS :
@@ -238,8 +288,19 @@ def get_local_driver(browser_name, headless, proxy_string):
238
288
chrome_options = _set_chrome_options (downloads_path , proxy_string )
239
289
if headless :
240
290
chrome_options .add_argument ("--headless" )
241
- return webdriver .Chrome (options = chrome_options )
291
+ chrome_options .add_argument ("--disable-gpu" )
292
+ chrome_options .add_argument ("--no-sandbox" )
293
+ if LOCAL_CHROMEDRIVER and os .path .exists (LOCAL_CHROMEDRIVER ):
294
+ make_driver_executable_if_not (LOCAL_CHROMEDRIVER )
295
+ return webdriver .Chrome (
296
+ executable_path = LOCAL_CHROMEDRIVER , options = chrome_options )
297
+ else :
298
+ return webdriver .Chrome (options = chrome_options )
242
299
except Exception as e :
243
300
if headless :
244
301
raise Exception (e )
245
- return webdriver .Chrome ()
302
+ if LOCAL_CHROMEDRIVER and os .path .exists (LOCAL_CHROMEDRIVER ):
303
+ make_driver_executable_if_not (LOCAL_CHROMEDRIVER )
304
+ return webdriver .Chrome (executable_path = LOCAL_CHROMEDRIVER )
305
+ else :
306
+ return webdriver .Chrome ()
0 commit comments