Skip to content

Commit d868b26

Browse files
committed
Refactoring Edge Chromium (version must be 80 or newer)
1 parent 5a5f6d6 commit d868b26

File tree

1 file changed

+184
-190
lines changed

1 file changed

+184
-190
lines changed

seleniumbase/core/browser_launcher.py

Lines changed: 184 additions & 190 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,202 +1212,196 @@ def get_local_driver(
12121212
"profile.managed_default_content_settings.popups": 0,
12131213
"profile.default_content_setting_values.automatic_downloads": 1,
12141214
}
1215-
try:
1216-
# Microsoft Edge (Chromium) version 79 or lower
1217-
chrome_options = _set_chrome_options(
1218-
browser_name,
1219-
downloads_path,
1220-
headless,
1221-
locale_code,
1222-
proxy_string,
1223-
proxy_auth,
1224-
proxy_user,
1225-
proxy_pass,
1226-
user_agent,
1227-
recorder_ext,
1228-
disable_csp,
1229-
enable_ws,
1230-
enable_sync,
1231-
use_auto_ext,
1232-
no_sandbox,
1233-
disable_gpu,
1234-
incognito,
1235-
guest_mode,
1236-
devtools,
1237-
remote_debug,
1238-
swiftshader,
1239-
ad_block_on,
1240-
block_images,
1241-
chromium_arg,
1242-
user_data_dir,
1243-
extension_zip,
1244-
extension_dir,
1245-
servername,
1246-
mobile_emulator,
1247-
device_width,
1248-
device_height,
1249-
device_pixel_ratio,
1250-
)
1251-
if LOCAL_EDGEDRIVER and os.path.exists(LOCAL_EDGEDRIVER):
1252-
try:
1253-
make_driver_executable_if_not(LOCAL_EDGEDRIVER)
1254-
except Exception as e:
1255-
logging.debug(
1256-
"\nWarning: Could not make edgedriver"
1257-
" executable: %s" % e
1258-
)
1259-
elif not is_edgedriver_on_path():
1260-
args = " ".join(sys.argv)
1261-
if not ("-n" in sys.argv or " -n=" in args or args == "-c"):
1262-
# (Not multithreaded)
1263-
from seleniumbase.console_scripts import sb_install
1215+
chrome_options = _set_chrome_options(
1216+
browser_name,
1217+
downloads_path,
1218+
headless,
1219+
locale_code,
1220+
proxy_string,
1221+
proxy_auth,
1222+
proxy_user,
1223+
proxy_pass,
1224+
user_agent,
1225+
recorder_ext,
1226+
disable_csp,
1227+
enable_ws,
1228+
enable_sync,
1229+
use_auto_ext,
1230+
no_sandbox,
1231+
disable_gpu,
1232+
incognito,
1233+
guest_mode,
1234+
devtools,
1235+
remote_debug,
1236+
swiftshader,
1237+
ad_block_on,
1238+
block_images,
1239+
chromium_arg,
1240+
user_data_dir,
1241+
extension_zip,
1242+
extension_dir,
1243+
servername,
1244+
mobile_emulator,
1245+
device_width,
1246+
device_height,
1247+
device_pixel_ratio,
1248+
)
1249+
if LOCAL_EDGEDRIVER and os.path.exists(LOCAL_EDGEDRIVER):
1250+
try:
1251+
make_driver_executable_if_not(LOCAL_EDGEDRIVER)
1252+
except Exception as e:
1253+
logging.debug(
1254+
"\nWarning: Could not make edgedriver"
1255+
" executable: %s" % e
1256+
)
1257+
elif not is_edgedriver_on_path():
1258+
args = " ".join(sys.argv)
1259+
if not ("-n" in sys.argv or " -n=" in args or args == "-c"):
1260+
# (Not multithreaded)
1261+
from seleniumbase.console_scripts import sb_install
12641262

1265-
sys_args = sys.argv # Save a copy of current sys args
1266-
print("\nWarning: msedgedriver not found. Installing now:")
1267-
sb_install.main(override="edgedriver")
1268-
sys.argv = sys_args # Put back the original sys args
1269-
# For Microsoft Edge (Chromium) version 79 or lower
1270-
return webdriver.Chrome(
1271-
executable_path=LOCAL_EDGEDRIVER, options=chrome_options
1272-
)
1273-
except Exception:
1274-
# For Microsoft Edge (Chromium) version 80 or higher
1275-
from msedge.selenium_tools import Edge, EdgeOptions
1263+
sys_args = sys.argv # Save a copy of current sys args
1264+
print("\nWarning: msedgedriver not found. Installing now:")
1265+
sb_install.main(override="edgedriver")
1266+
sys.argv = sys_args # Put back the original sys args
12761267

1277-
if LOCAL_EDGEDRIVER and os.path.exists(LOCAL_EDGEDRIVER):
1278-
try:
1279-
make_driver_executable_if_not(LOCAL_EDGEDRIVER)
1280-
except Exception as e:
1281-
logging.debug(
1282-
"\nWarning: Could not make edgedriver"
1283-
" executable: %s" % e
1284-
)
1285-
edge_options = EdgeOptions()
1286-
edge_options.use_chromium = True
1287-
if locale_code:
1288-
prefs["intl.accept_languages"] = locale_code
1289-
if block_images:
1290-
prefs["profile.managed_default_content_settings.images"] = 2
1291-
edge_options.add_experimental_option("prefs", prefs)
1292-
edge_options.add_experimental_option("w3c", True)
1293-
edge_options.add_argument(
1294-
"--disable-blink-features=AutomationControlled"
1295-
)
1296-
edge_options.add_experimental_option(
1297-
"useAutomationExtension", False
1298-
)
1299-
edge_options.add_experimental_option(
1300-
"excludeSwitches", ["enable-automation", "enable-logging"]
1301-
)
1302-
if guest_mode:
1303-
edge_options.add_argument("--guest")
1304-
if headless:
1305-
edge_options.add_argument("--headless")
1306-
if mobile_emulator:
1307-
emulator_settings = {}
1308-
device_metrics = {}
1309-
if (
1310-
type(device_width) is int
1311-
and type(device_height) is int
1312-
and type(device_pixel_ratio) is int
1313-
):
1314-
device_metrics["width"] = device_width
1315-
device_metrics["height"] = device_height
1316-
device_metrics["pixelRatio"] = device_pixel_ratio
1317-
else:
1318-
device_metrics["width"] = 411
1319-
device_metrics["height"] = 731
1320-
device_metrics["pixelRatio"] = 3
1321-
emulator_settings["deviceMetrics"] = device_metrics
1322-
if user_agent:
1323-
emulator_settings["userAgent"] = user_agent
1324-
edge_options.add_experimental_option(
1325-
"mobileEmulation", emulator_settings
1268+
# For Microsoft Edge (Chromium) version 80 or higher
1269+
from msedge.selenium_tools import Edge, EdgeOptions
1270+
1271+
if LOCAL_EDGEDRIVER and os.path.exists(LOCAL_EDGEDRIVER):
1272+
try:
1273+
make_driver_executable_if_not(LOCAL_EDGEDRIVER)
1274+
except Exception as e:
1275+
logging.debug(
1276+
"\nWarning: Could not make edgedriver"
1277+
" executable: %s" % e
13261278
)
1327-
if user_data_dir:
1328-
abs_path = os.path.abspath(user_data_dir)
1329-
edge_options.add_argument("user-data-dir=%s" % abs_path)
1330-
if extension_zip:
1331-
# Can be a comma-separated list of .ZIP or .CRX files
1332-
extension_zip_list = extension_zip.split(",")
1333-
for extension_zip_item in extension_zip_list:
1334-
abs_path = os.path.abspath(extension_zip_item)
1335-
edge_options.add_extension(abs_path)
1336-
if extension_dir:
1337-
# load-extension input can be a comma-separated list
1338-
abs_path = os.path.abspath(extension_dir)
1339-
edge_options.add_argument("--load-extension=%s" % abs_path)
1340-
edge_options.add_argument("--disable-infobars")
1341-
edge_options.add_argument("--disable-notifications")
1342-
edge_options.add_argument("--disable-save-password-bubble")
1343-
edge_options.add_argument("--disable-single-click-autofill")
1344-
edge_options.add_argument(
1345-
"--disable-autofill-keyboard-accessory-view[8]"
1346-
)
1347-
edge_options.add_argument("--disable-translate")
1348-
if not enable_ws:
1349-
edge_options.add_argument("--disable-web-security")
1350-
edge_options.add_argument("--homepage=about:blank")
1351-
edge_options.add_argument("--dns-prefetch-disable")
1352-
edge_options.add_argument("--dom-automation")
1353-
edge_options.add_argument("--disable-hang-monitor")
1354-
edge_options.add_argument("--disable-prompt-on-repost")
1279+
edge_options = EdgeOptions()
1280+
edge_options.use_chromium = True
1281+
if locale_code:
1282+
prefs["intl.accept_languages"] = locale_code
1283+
if block_images:
1284+
prefs["profile.managed_default_content_settings.images"] = 2
1285+
edge_options.add_experimental_option("prefs", prefs)
1286+
edge_options.add_experimental_option("w3c", True)
1287+
edge_options.add_argument(
1288+
"--disable-blink-features=AutomationControlled"
1289+
)
1290+
edge_options.add_experimental_option(
1291+
"useAutomationExtension", False
1292+
)
1293+
edge_options.add_experimental_option(
1294+
"excludeSwitches", ["enable-automation", "enable-logging"]
1295+
)
1296+
if guest_mode:
1297+
edge_options.add_argument("--guest")
1298+
if headless:
1299+
edge_options.add_argument("--headless")
1300+
if mobile_emulator:
1301+
emulator_settings = {}
1302+
device_metrics = {}
13551303
if (
1356-
settings.DISABLE_CSP_ON_CHROME or disable_csp
1357-
) and not headless:
1358-
# Headless Edge doesn't support extensions, which are required
1359-
# for disabling the Content Security Policy on Edge
1360-
edge_options = _add_chrome_disable_csp_extension(edge_options)
1361-
if ad_block_on and not headless:
1362-
edge_options = _add_chrome_ad_block_extension(edge_options)
1363-
if recorder_ext and not headless:
1364-
edge_options = _add_chrome_recorder_extension(edge_options)
1365-
if proxy_string:
1366-
if proxy_auth:
1367-
edge_options = _add_chrome_proxy_extension(
1368-
edge_options, proxy_string, proxy_user, proxy_pass
1369-
)
1370-
edge_options.add_argument("--proxy-server=%s" % proxy_string)
1371-
edge_options.add_argument("--test-type")
1372-
edge_options.add_argument("--log-level=3")
1373-
edge_options.add_argument("--no-first-run")
1374-
edge_options.add_argument("--ignore-certificate-errors")
1375-
if devtools and not headless:
1376-
edge_options.add_argument("--auto-open-devtools-for-tabs")
1377-
edge_options.add_argument("--allow-file-access-from-files")
1378-
edge_options.add_argument("--allow-insecure-localhost")
1379-
edge_options.add_argument("--allow-running-insecure-content")
1380-
if user_agent:
1381-
edge_options.add_argument("--user-agent=%s" % user_agent)
1382-
edge_options.add_argument("--no-sandbox")
1383-
if remote_debug:
1384-
# To access the Remote Debugger, go to: http://localhost:9222
1385-
# while a Chromium driver is running.
1386-
# Info: https://chromedevtools.github.io/devtools-protocol/
1387-
edge_options.add_argument("--remote-debugging-port=9222")
1388-
if swiftshader:
1389-
edge_options.add_argument("--use-gl=swiftshader")
1304+
type(device_width) is int
1305+
and type(device_height) is int
1306+
and type(device_pixel_ratio) is int
1307+
):
1308+
device_metrics["width"] = device_width
1309+
device_metrics["height"] = device_height
1310+
device_metrics["pixelRatio"] = device_pixel_ratio
13901311
else:
1391-
edge_options.add_argument("--disable-gpu")
1392-
if "linux" in PLATFORM:
1393-
edge_options.add_argument("--disable-dev-shm-usage")
1394-
if chromium_arg:
1395-
# Can be a comma-separated list of Chromium args
1396-
chromium_arg_list = chromium_arg.split(",")
1397-
for chromium_arg_item in chromium_arg_list:
1398-
chromium_arg_item = chromium_arg_item.strip()
1399-
if not chromium_arg_item.startswith("--"):
1400-
if chromium_arg_item.startswith("-"):
1401-
chromium_arg_item = "-" + chromium_arg_item
1402-
else:
1403-
chromium_arg_item = "--" + chromium_arg_item
1404-
if len(chromium_arg_item) >= 3:
1405-
edge_options.add_argument(chromium_arg_item)
1406-
capabilities = edge_options.to_capabilities()
1407-
capabilities["platform"] = ""
1408-
return Edge(
1409-
executable_path=LOCAL_EDGEDRIVER, capabilities=capabilities
1312+
device_metrics["width"] = 411
1313+
device_metrics["height"] = 731
1314+
device_metrics["pixelRatio"] = 3
1315+
emulator_settings["deviceMetrics"] = device_metrics
1316+
if user_agent:
1317+
emulator_settings["userAgent"] = user_agent
1318+
edge_options.add_experimental_option(
1319+
"mobileEmulation", emulator_settings
14101320
)
1321+
if user_data_dir:
1322+
abs_path = os.path.abspath(user_data_dir)
1323+
edge_options.add_argument("user-data-dir=%s" % abs_path)
1324+
if extension_zip:
1325+
# Can be a comma-separated list of .ZIP or .CRX files
1326+
extension_zip_list = extension_zip.split(",")
1327+
for extension_zip_item in extension_zip_list:
1328+
abs_path = os.path.abspath(extension_zip_item)
1329+
edge_options.add_extension(abs_path)
1330+
if extension_dir:
1331+
# load-extension input can be a comma-separated list
1332+
abs_path = os.path.abspath(extension_dir)
1333+
edge_options.add_argument("--load-extension=%s" % abs_path)
1334+
edge_options.add_argument("--disable-infobars")
1335+
edge_options.add_argument("--disable-notifications")
1336+
edge_options.add_argument("--disable-save-password-bubble")
1337+
edge_options.add_argument("--disable-single-click-autofill")
1338+
edge_options.add_argument(
1339+
"--disable-autofill-keyboard-accessory-view[8]"
1340+
)
1341+
edge_options.add_argument("--disable-translate")
1342+
if not enable_ws:
1343+
edge_options.add_argument("--disable-web-security")
1344+
edge_options.add_argument("--homepage=about:blank")
1345+
edge_options.add_argument("--dns-prefetch-disable")
1346+
edge_options.add_argument("--dom-automation")
1347+
edge_options.add_argument("--disable-hang-monitor")
1348+
edge_options.add_argument("--disable-prompt-on-repost")
1349+
if (
1350+
settings.DISABLE_CSP_ON_CHROME or disable_csp
1351+
) and not headless:
1352+
# Headless Edge doesn't support extensions, which are required
1353+
# for disabling the Content Security Policy on Edge
1354+
edge_options = _add_chrome_disable_csp_extension(edge_options)
1355+
if ad_block_on and not headless:
1356+
edge_options = _add_chrome_ad_block_extension(edge_options)
1357+
if recorder_ext and not headless:
1358+
edge_options = _add_chrome_recorder_extension(edge_options)
1359+
if proxy_string:
1360+
if proxy_auth:
1361+
edge_options = _add_chrome_proxy_extension(
1362+
edge_options, proxy_string, proxy_user, proxy_pass
1363+
)
1364+
edge_options.add_argument("--proxy-server=%s" % proxy_string)
1365+
edge_options.add_argument("--test-type")
1366+
edge_options.add_argument("--log-level=3")
1367+
edge_options.add_argument("--no-first-run")
1368+
edge_options.add_argument("--ignore-certificate-errors")
1369+
if devtools and not headless:
1370+
edge_options.add_argument("--auto-open-devtools-for-tabs")
1371+
edge_options.add_argument("--allow-file-access-from-files")
1372+
edge_options.add_argument("--allow-insecure-localhost")
1373+
edge_options.add_argument("--allow-running-insecure-content")
1374+
if user_agent:
1375+
edge_options.add_argument("--user-agent=%s" % user_agent)
1376+
edge_options.add_argument("--no-sandbox")
1377+
if remote_debug:
1378+
# To access the Remote Debugger, go to: http://localhost:9222
1379+
# while a Chromium driver is running.
1380+
# Info: https://chromedevtools.github.io/devtools-protocol/
1381+
edge_options.add_argument("--remote-debugging-port=9222")
1382+
if swiftshader:
1383+
edge_options.add_argument("--use-gl=swiftshader")
1384+
else:
1385+
edge_options.add_argument("--disable-gpu")
1386+
if "linux" in PLATFORM:
1387+
edge_options.add_argument("--disable-dev-shm-usage")
1388+
if chromium_arg:
1389+
# Can be a comma-separated list of Chromium args
1390+
chromium_arg_list = chromium_arg.split(",")
1391+
for chromium_arg_item in chromium_arg_list:
1392+
chromium_arg_item = chromium_arg_item.strip()
1393+
if not chromium_arg_item.startswith("--"):
1394+
if chromium_arg_item.startswith("-"):
1395+
chromium_arg_item = "-" + chromium_arg_item
1396+
else:
1397+
chromium_arg_item = "--" + chromium_arg_item
1398+
if len(chromium_arg_item) >= 3:
1399+
edge_options.add_argument(chromium_arg_item)
1400+
capabilities = edge_options.to_capabilities()
1401+
capabilities["platform"] = ""
1402+
return Edge(
1403+
executable_path=LOCAL_EDGEDRIVER, capabilities=capabilities
1404+
)
14111405
elif browser_name == constants.Browser.SAFARI:
14121406
arg_join = " ".join(sys.argv)
14131407
if ("-n" in sys.argv) or (" -n=" in arg_join) or (arg_join == "-c"):

0 commit comments

Comments
 (0)