Skip to content

Commit 0571749

Browse files
committed
Use local web drivers from the "drivers" folder if available
1 parent 78ef1d6 commit 0571749

File tree

1 file changed

+67
-6
lines changed

1 file changed

+67
-6
lines changed

seleniumbase/core/browser_launcher.py

Lines changed: 67 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import os
12
import re
3+
import sys
24
import warnings
35
from selenium import webdriver
46
from selenium.common.exceptions import WebDriverException
@@ -8,6 +10,36 @@
810
from seleniumbase.core import download_helper
911
from seleniumbase.fixtures import constants
1012
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)
1143

1244

1345
def _set_chrome_options(downloads_path, proxy_string):
@@ -132,6 +164,8 @@ def get_remote_driver(browser_name, headless, servername, port, proxy_string):
132164
chrome_options = _set_chrome_options(downloads_path, proxy_string)
133165
if headless:
134166
chrome_options.add_argument("--headless")
167+
chrome_options.add_argument("--disable-gpu")
168+
chrome_options.add_argument("--no-sandbox")
135169
capabilities = chrome_options.to_capabilities()
136170
return webdriver.Remote(
137171
command_executor=address,
@@ -207,9 +241,18 @@ def get_local_driver(browser_name, headless, proxy_string):
207241
options = webdriver.FirefoxOptions()
208242
if headless:
209243
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)
213256
except WebDriverException:
214257
# Don't use Geckodriver: Only works for old versions of Firefox
215258
profile = _create_firefox_profile(downloads_path, proxy_string)
@@ -225,7 +268,14 @@ def get_local_driver(browser_name, headless, proxy_string):
225268
elif browser_name == constants.Browser.INTERNET_EXPLORER:
226269
return webdriver.Ie()
227270
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)
229279
elif browser_name == constants.Browser.SAFARI:
230280
return webdriver.Safari()
231281
elif browser_name == constants.Browser.PHANTOM_JS:
@@ -238,8 +288,19 @@ def get_local_driver(browser_name, headless, proxy_string):
238288
chrome_options = _set_chrome_options(downloads_path, proxy_string)
239289
if headless:
240290
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)
242299
except Exception as e:
243300
if headless:
244301
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

Comments
 (0)