Skip to content

Commit ffae4f8

Browse files
committed
Optimize browser-launching
1 parent 9c5e9ee commit ffae4f8

File tree

1 file changed

+59
-31
lines changed

1 file changed

+59
-31
lines changed

seleniumbase/core/browser_launcher.py

Lines changed: 59 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
import urllib3
66
import warnings
77
from selenium import webdriver
8+
from selenium.webdriver.chrome.service import Service as Chrome_Service
9+
from selenium.webdriver.edge.service import Service as Edge_Service
10+
from selenium.webdriver.firefox.service import Service as Firefox_Service
811
from seleniumbase.config import settings
912
from seleniumbase.core import download_helper
1013
from seleniumbase.core import proxy_helper
@@ -13,6 +16,9 @@
1316
from seleniumbase import extensions # browser extensions storage folder
1417

1518
urllib3.disable_warnings()
19+
selenium4 = False
20+
if sys.version_info[0] == 3 and sys.version_info[1] >= 7:
21+
selenium4 = True
1622
DRIVER_DIR = os.path.dirname(os.path.realpath(drivers.__file__))
1723
# Make sure that the SeleniumBase DRIVER_DIR is at the top of the System PATH
1824
# (Changes to the System PATH with os.environ only last during the test run)
@@ -116,7 +122,18 @@ def _repair_chromedriver(chrome_options, headless_options):
116122
"sbase install chromedriver 2.44", shell=True
117123
)
118124
try:
119-
driver = webdriver.Chrome(options=headless_options)
125+
if selenium4:
126+
service = Chrome_Service(
127+
executable_path=LOCAL_CHROMEDRIVER)
128+
driver = webdriver.Chrome(
129+
service=service,
130+
options=headless_options,
131+
)
132+
else:
133+
driver = webdriver.Chrome(
134+
executable_path=LOCAL_CHROMEDRIVER,
135+
options=headless_options,
136+
)
120137
except Exception:
121138
subprocess.check_call(
122139
"sbase install chromedriver latest-1", shell=True
@@ -1132,11 +1149,18 @@ def get_local_driver(
11321149
)
11331150
else:
11341151
if os.path.exists(LOCAL_GECKODRIVER):
1135-
warnings.simplefilter("ignore", category=DeprecationWarning)
1136-
return webdriver.Firefox(
1137-
executable_path=LOCAL_GECKODRIVER,
1138-
options=firefox_options,
1139-
)
1152+
if selenium4:
1153+
service = Firefox_Service(
1154+
executable_path=LOCAL_GECKODRIVER)
1155+
return webdriver.Firefox(
1156+
service=service,
1157+
options=firefox_options,
1158+
)
1159+
else:
1160+
return webdriver.Firefox(
1161+
executable_path=LOCAL_GECKODRIVER,
1162+
options=firefox_options,
1163+
)
11401164
else:
11411165
return webdriver.Firefox(options=firefox_options)
11421166
elif browser_name == constants.Browser.INTERNET_EXPLORER:
@@ -1269,10 +1293,6 @@ def get_local_driver(
12691293
sb_install.main(override="edgedriver")
12701294
sys.argv = sys_args # Put back the original sys args
12711295

1272-
selenium4 = False
1273-
if sys.version_info[0] == 3 and sys.version_info[1] >= 7:
1274-
selenium4 = True
1275-
12761296
# For Microsoft Edge (Chromium) version 80 or higher
12771297
if selenium4:
12781298
Edge = webdriver.edge.webdriver.WebDriver
@@ -1410,12 +1430,9 @@ def get_local_driver(
14101430
if len(chromium_arg_item) >= 3:
14111431
edge_options.add_argument(chromium_arg_item)
14121432
if selenium4:
1413-
warnings.simplefilter("ignore", category=DeprecationWarning)
14141433
try:
1415-
driver = Edge(
1416-
executable_path=LOCAL_EDGEDRIVER,
1417-
options=edge_options,
1418-
)
1434+
service = Edge_Service(executable_path=LOCAL_EDGEDRIVER)
1435+
driver = Edge(service=service, options=edge_options)
14191436
except Exception as e:
14201437
auto_upgrade_edgedriver = False
14211438
if "This version of MSEdgeDriver only supports" in e.msg:
@@ -1442,10 +1459,8 @@ def get_local_driver(
14421459
if not _was_chromedriver_repaired(): # Works for Edge
14431460
_repair_edgedriver(edge_version)
14441461
_mark_chromedriver_repaired() # Works for Edge
1445-
driver = Edge(
1446-
executable_path=LOCAL_EDGEDRIVER,
1447-
options=edge_options,
1448-
)
1462+
service = Edge_Service(executable_path=LOCAL_EDGEDRIVER)
1463+
driver = Edge(service=service, options=edge_options)
14491464
return driver
14501465
else:
14511466
capabilities = edge_options.to_capabilities()
@@ -1538,6 +1553,7 @@ def get_local_driver(
15381553
device_pixel_ratio,
15391554
)
15401555
opera_options.headless = False # No support for headless Opera
1556+
warnings.simplefilter("ignore", category=DeprecationWarning)
15411557
return webdriver.Opera(options=opera_options)
15421558
except Exception:
15431559
return webdriver.Opera()
@@ -1619,12 +1635,18 @@ def get_local_driver(
16191635
if not headless or "linux" not in PLATFORM:
16201636
try:
16211637
if os.path.exists(LOCAL_CHROMEDRIVER):
1622-
warnings.simplefilter(
1623-
"ignore", category=DeprecationWarning)
1624-
driver = webdriver.Chrome(
1625-
executable_path=LOCAL_CHROMEDRIVER,
1626-
options=chrome_options,
1627-
)
1638+
if selenium4:
1639+
service = Chrome_Service(
1640+
executable_path=LOCAL_CHROMEDRIVER)
1641+
driver = webdriver.Chrome(
1642+
service=service,
1643+
options=chrome_options,
1644+
)
1645+
else:
1646+
driver = webdriver.Chrome(
1647+
executable_path=LOCAL_CHROMEDRIVER,
1648+
options=chrome_options,
1649+
)
16281650
else:
16291651
driver = webdriver.Chrome(options=chrome_options)
16301652
except Exception as e:
@@ -1692,12 +1714,18 @@ def get_local_driver(
16921714
)
16931715
_mark_chromedriver_repaired()
16941716
if os.path.exists(LOCAL_CHROMEDRIVER):
1695-
warnings.simplefilter(
1696-
"ignore", category=DeprecationWarning)
1697-
driver = webdriver.Chrome(
1698-
executable_path=LOCAL_CHROMEDRIVER,
1699-
options=chrome_options,
1700-
)
1717+
if selenium4:
1718+
service = Chrome_Service(
1719+
executable_path=LOCAL_CHROMEDRIVER)
1720+
driver = webdriver.Chrome(
1721+
service=service,
1722+
options=chrome_options,
1723+
)
1724+
else:
1725+
driver = webdriver.Chrome(
1726+
executable_path=LOCAL_CHROMEDRIVER,
1727+
options=chrome_options,
1728+
)
17011729
else:
17021730
driver = webdriver.Chrome(options=chrome_options)
17031731
return driver

0 commit comments

Comments
 (0)