Refactor driver settings

This commit is contained in:
Michael Mintz 2023-08-21 23:06:21 -04:00
parent e25d73faeb
commit 96cd45bc4e
4 changed files with 95 additions and 18 deletions

View File

@ -18,6 +18,7 @@ from selenium.webdriver.safari.service import Service as SafariService
from seleniumbase import drivers # webdriver storage folder for SeleniumBase from seleniumbase import drivers # webdriver storage folder for SeleniumBase
from seleniumbase import extensions # browser extensions storage folder from seleniumbase import extensions # browser extensions storage folder
from seleniumbase.config import settings from seleniumbase.config import settings
from seleniumbase.core import detect_b_ver
from seleniumbase.core import download_helper from seleniumbase.core import download_helper
from seleniumbase.core import proxy_helper from seleniumbase.core import proxy_helper
from seleniumbase.fixtures import constants from seleniumbase.fixtures import constants
@ -190,8 +191,10 @@ def has_cf(text):
if ( if (
"<title>Just a moment...</title>" in text "<title>Just a moment...</title>" in text
or "<title>403 Forbidden</title>" in text or "<title>403 Forbidden</title>" in text
or "Permission Denied</title>" in text
or 'id="challenge-error-text"' in text or 'id="challenge-error-text"' in text
or 'action="/?__cf_chl_f_tk' in text or 'action="/?__cf_chl_f_tk' in text
or 'src="chromedriver.js"' in text
or 'id="challenge-form"' in text or 'id="challenge-form"' in text
or "window._cf_chl_opt" in text or "window._cf_chl_opt" in text
): ):
@ -312,7 +315,7 @@ def get_valid_binary_names_for_browser(browser):
else: else:
raise Exception("Could not determine OS, or unsupported!") raise Exception("Could not determine OS, or unsupported!")
else: else:
raise Exception("Invalid combination for os browser binaries!") raise Exception("Invalid combination for OS browser binaries!")
def _repair_chromedriver(chrome_options, headless_options, mcv=None): def _repair_chromedriver(chrome_options, headless_options, mcv=None):
@ -870,7 +873,12 @@ def _set_chrome_options(
) )
except Exception: except Exception:
pass pass
if len(chromium_arg_item) >= 3: if "set-binary" in chromium_arg_item and not binary_location:
br_app = "google-chrome"
binary_loc = detect_b_ver.get_binary_location(br_app)
if os.path.exists(binary_loc):
binary_location = binary_loc
elif len(chromium_arg_item) >= 3:
chrome_options.add_argument(chromium_arg_item) chrome_options.add_argument(chromium_arg_item)
if devtools and not headless: if devtools and not headless:
chrome_options.add_argument("--auto-open-devtools-for-tabs") chrome_options.add_argument("--auto-open-devtools-for-tabs")
@ -1166,6 +1174,8 @@ def get_driver(
"that includes the driver filename at the end of it!" "that includes the driver filename at the end of it!"
"\n(Will use default settings...)\n" % binary_location "\n(Will use default settings...)\n" % binary_location
) )
# Example of a valid binary location path - MacOS:
# "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
binary_location = None binary_location = None
else: else:
binary_name = binary_location.split("/")[-1].split("\\")[-1] binary_name = binary_location.split("/")[-1].split("\\")[-1]
@ -2251,8 +2261,6 @@ def get_local_driver(
use_version = "latest" use_version = "latest"
major_edge_version = None major_edge_version = None
try: try:
from seleniumbase.core import detect_b_ver
if binary_location: if binary_location:
try: try:
major_edge_version = ( major_edge_version = (
@ -2480,8 +2488,6 @@ def get_local_driver(
) )
edge_options.add_argument("--disable-browser-side-navigation") edge_options.add_argument("--disable-browser-side-navigation")
edge_options.add_argument("--disable-translate") edge_options.add_argument("--disable-translate")
if binary_location:
edge_options.binary_location = binary_location
if not enable_ws: if not enable_ws:
edge_options.add_argument("--disable-web-security") edge_options.add_argument("--disable-web-security")
edge_options.add_argument("--homepage=about:blank") edge_options.add_argument("--homepage=about:blank")
@ -2585,8 +2591,15 @@ def get_local_driver(
chromium_arg_item = "-" + chromium_arg_item chromium_arg_item = "-" + chromium_arg_item
else: else:
chromium_arg_item = "--" + chromium_arg_item chromium_arg_item = "--" + chromium_arg_item
if len(chromium_arg_item) >= 3: if "set-binary" in chromium_arg_item and not binary_location:
br_app = "edge"
binary_loc = detect_b_ver.get_binary_location(br_app)
if os.path.exists(binary_loc):
binary_location = binary_loc
elif len(chromium_arg_item) >= 3:
edge_options.add_argument(chromium_arg_item) edge_options.add_argument(chromium_arg_item)
if binary_location:
edge_options.binary_location = binary_location
if selenium4_or_newer: if selenium4_or_newer:
try: try:
service = EdgeService( service = EdgeService(
@ -2884,8 +2897,6 @@ def get_local_driver(
major_chrome_version = None major_chrome_version = None
if selenium4_or_newer: if selenium4_or_newer:
try: try:
from seleniumbase.core import detect_b_ver
if binary_location: if binary_location:
try: try:
major_chrome_version = ( major_chrome_version = (

View File

@ -106,6 +106,68 @@ def windows_browser_apps_to_cmd(*apps):
return '%s -NoProfile "%s"' % (powershell, script) return '%s -NoProfile "%s"' % (powershell, script)
def get_binary_location(browser_type):
cmd_mapping = {
ChromeType.GOOGLE: {
OSType.LINUX: linux_browser_apps_to_cmd(
"google-chrome-stable",
"google-chrome",
"chrome",
"chromium",
"chromium-browser",
"google-chrome-beta",
"google-chrome-dev",
"google-chrome-unstable",
),
OSType.MAC: r"/Applications/Google Chrome.app"
r"/Contents/MacOS/Google Chrome",
OSType.WIN: windows_browser_apps_to_cmd(
r'(Get-Item -Path "$env:PROGRAMFILES\Google\Chrome'
r'\Application\chrome.exe")',
r'(Get-Item -Path "$env:PROGRAMFILES (x86)\Google\Chrome'
r'\Application\chrome.exe")',
r'(Get-Item -Path "$env:LOCALAPPDATA\Google\Chrome'
r'\Application\chrome.exe")',
),
},
ChromeType.MSEDGE: {
OSType.LINUX: linux_browser_apps_to_cmd(
"microsoft-edge-stable",
"microsoft-edge",
"microsoft-edge-beta",
"microsoft-edge-dev",
),
OSType.MAC: r"/Applications/Microsoft Edge.app"
r"/Contents/MacOS/Microsoft Edge",
OSType.WIN: windows_browser_apps_to_cmd(
# stable edge
r'(Get-Item -Path "$env:PROGRAMFILES\Microsoft\Edge'
r'\Application\msedge.exe")',
r'(Get-Item -Path "$env:PROGRAMFILES (x86)\Microsoft'
r'\Edge\Application\msedge.exe")',
# beta edge
r'(Get-Item -Path "$env:LOCALAPPDATA\Microsoft\Edge Beta'
r'\Application\msedge.exe")',
r'(Get-Item -Path "$env:PROGRAMFILES\Microsoft\Edge Beta'
r'\Application\msedge.exe")',
r'(Get-Item -Path "$env:PROGRAMFILES (x86)\Microsoft\Edge Beta'
r'\Application\msedge.exe")',
# dev edge
r'(Get-Item -Path "$env:LOCALAPPDATA\Microsoft\Edge Dev'
r'\Application\msedge.exe")',
r'(Get-Item -Path "$env:PROGRAMFILES\Microsoft\Edge Dev'
r'\Application\msedge.exe")',
r'(Get-Item -Path "$env:PROGRAMFILES (x86)\Microsoft\Edge Dev'
r'\Application\msedge.exe")',
# canary edge
r'(Get-Item -Path "$env:LOCALAPPDATA\Microsoft\Edge SxS'
r'\Application\msedge.exe")',
),
},
}
return cmd_mapping[browser_type][os_name()]
def get_browser_version_from_binary(binary_location): def get_browser_version_from_binary(binary_location):
try: try:
if binary_location.count(r"\ ") != binary_location.count(" "): if binary_location.count(r"\ ") != binary_location.count(" "):
@ -118,18 +180,19 @@ def get_browser_version_from_binary(binary_location):
return None return None
def get_browser_version_from_os(browser_type=None): def get_browser_version_from_os(browser_type):
"""Return installed browser version.""" """Return installed browser version."""
cmd_mapping = { cmd_mapping = {
ChromeType.GOOGLE: { ChromeType.GOOGLE: {
OSType.LINUX: linux_browser_apps_to_cmd( OSType.LINUX: linux_browser_apps_to_cmd(
"google-chrome",
"google-chrome-stable", "google-chrome-stable",
"google-chrome",
"chrome", "chrome",
"chromium", "chromium",
"chromium-browser",
"google-chrome-beta", "google-chrome-beta",
"google-chrome-dev", "google-chrome-dev",
"chromium-browser", "google-chrome-unstable",
), ),
OSType.MAC: r"/Applications/Google\ Chrome.app" OSType.MAC: r"/Applications/Google\ Chrome.app"
r"/Contents/MacOS/Google\ Chrome --version", r"/Contents/MacOS/Google\ Chrome --version",
@ -149,8 +212,8 @@ def get_browser_version_from_os(browser_type=None):
}, },
ChromeType.MSEDGE: { ChromeType.MSEDGE: {
OSType.LINUX: linux_browser_apps_to_cmd( OSType.LINUX: linux_browser_apps_to_cmd(
"microsoft-edge",
"microsoft-edge-stable", "microsoft-edge-stable",
"microsoft-edge",
"microsoft-edge-beta", "microsoft-edge-beta",
"microsoft-edge-dev", "microsoft-edge-dev",
), ),

View File

@ -360,23 +360,25 @@ class ValidBrowsers:
class ValidBinaries: class ValidBinaries:
valid_chrome_binaries_on_linux = [ valid_chrome_binaries_on_linux = [
"google-chrome",
"google-chrome-stable", "google-chrome-stable",
"google-chrome",
"chrome", "chrome",
"chromium", "chromium",
"chromium-browser",
"google-chrome-beta", "google-chrome-beta",
"google-chrome-dev", "google-chrome-dev",
"chromium-browser", "google-chrome-unstable",
] ]
valid_edge_binaries_on_linux = [ valid_edge_binaries_on_linux = [
"microsoft-edge",
"microsoft-edge-stable", "microsoft-edge-stable",
"microsoft-edge",
"microsoft-edge-beta", "microsoft-edge-beta",
"microsoft-edge-dev", "microsoft-edge-dev",
] ]
valid_chrome_binaries_on_macos = [ valid_chrome_binaries_on_macos = [
"Google Chrome", "Google Chrome",
"Chromium", "Chromium",
"Google Chrome for Testing",
] ]
valid_edge_binaries_on_macos = [ valid_edge_binaries_on_macos = [
"Microsoft Edge", "Microsoft Edge",

View File

@ -506,13 +506,14 @@ def find_chrome_executable():
if IS_POSIX: if IS_POSIX:
for item in os.environ.get("PATH").split(os.pathsep): for item in os.environ.get("PATH").split(os.pathsep):
for subitem in ( for subitem in (
"google-chrome",
"google-chrome-stable", "google-chrome-stable",
"google-chrome",
"chrome", "chrome",
"chromium", "chromium",
"chromium-browser",
"google-chrome-beta", "google-chrome-beta",
"google-chrome-dev", "google-chrome-dev",
"chromium-browser", "google-chrome-unstable",
): ):
candidates.add(os.sep.join((item, subitem))) candidates.add(os.sep.join((item, subitem)))
if "darwin" in sys.platform: if "darwin" in sys.platform: