Skip to content

Commit 96cd45b

Browse files
committed
Refactor driver settings
1 parent e25d73f commit 96cd45b

File tree

4 files changed

+95
-18
lines changed

4 files changed

+95
-18
lines changed

seleniumbase/core/browser_launcher.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from seleniumbase import drivers # webdriver storage folder for SeleniumBase
1919
from seleniumbase import extensions # browser extensions storage folder
2020
from seleniumbase.config import settings
21+
from seleniumbase.core import detect_b_ver
2122
from seleniumbase.core import download_helper
2223
from seleniumbase.core import proxy_helper
2324
from seleniumbase.fixtures import constants
@@ -190,8 +191,10 @@ def has_cf(text):
190191
if (
191192
"<title>Just a moment...</title>" in text
192193
or "<title>403 Forbidden</title>" in text
194+
or "Permission Denied</title>" in text
193195
or 'id="challenge-error-text"' in text
194196
or 'action="/?__cf_chl_f_tk' in text
197+
or 'src="chromedriver.js"' in text
195198
or 'id="challenge-form"' in text
196199
or "window._cf_chl_opt" in text
197200
):
@@ -312,7 +315,7 @@ def get_valid_binary_names_for_browser(browser):
312315
else:
313316
raise Exception("Could not determine OS, or unsupported!")
314317
else:
315-
raise Exception("Invalid combination for os browser binaries!")
318+
raise Exception("Invalid combination for OS browser binaries!")
316319

317320

318321
def _repair_chromedriver(chrome_options, headless_options, mcv=None):
@@ -870,7 +873,12 @@ def _set_chrome_options(
870873
)
871874
except Exception:
872875
pass
873-
if len(chromium_arg_item) >= 3:
876+
if "set-binary" in chromium_arg_item and not binary_location:
877+
br_app = "google-chrome"
878+
binary_loc = detect_b_ver.get_binary_location(br_app)
879+
if os.path.exists(binary_loc):
880+
binary_location = binary_loc
881+
elif len(chromium_arg_item) >= 3:
874882
chrome_options.add_argument(chromium_arg_item)
875883
if devtools and not headless:
876884
chrome_options.add_argument("--auto-open-devtools-for-tabs")
@@ -1166,6 +1174,8 @@ def get_driver(
11661174
"that includes the driver filename at the end of it!"
11671175
"\n(Will use default settings...)\n" % binary_location
11681176
)
1177+
# Example of a valid binary location path - MacOS:
1178+
# "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
11691179
binary_location = None
11701180
else:
11711181
binary_name = binary_location.split("/")[-1].split("\\")[-1]
@@ -2251,8 +2261,6 @@ def get_local_driver(
22512261
use_version = "latest"
22522262
major_edge_version = None
22532263
try:
2254-
from seleniumbase.core import detect_b_ver
2255-
22562264
if binary_location:
22572265
try:
22582266
major_edge_version = (
@@ -2480,8 +2488,6 @@ def get_local_driver(
24802488
)
24812489
edge_options.add_argument("--disable-browser-side-navigation")
24822490
edge_options.add_argument("--disable-translate")
2483-
if binary_location:
2484-
edge_options.binary_location = binary_location
24852491
if not enable_ws:
24862492
edge_options.add_argument("--disable-web-security")
24872493
edge_options.add_argument("--homepage=about:blank")
@@ -2585,8 +2591,15 @@ def get_local_driver(
25852591
chromium_arg_item = "-" + chromium_arg_item
25862592
else:
25872593
chromium_arg_item = "--" + chromium_arg_item
2588-
if len(chromium_arg_item) >= 3:
2594+
if "set-binary" in chromium_arg_item and not binary_location:
2595+
br_app = "edge"
2596+
binary_loc = detect_b_ver.get_binary_location(br_app)
2597+
if os.path.exists(binary_loc):
2598+
binary_location = binary_loc
2599+
elif len(chromium_arg_item) >= 3:
25892600
edge_options.add_argument(chromium_arg_item)
2601+
if binary_location:
2602+
edge_options.binary_location = binary_location
25902603
if selenium4_or_newer:
25912604
try:
25922605
service = EdgeService(
@@ -2884,8 +2897,6 @@ def get_local_driver(
28842897
major_chrome_version = None
28852898
if selenium4_or_newer:
28862899
try:
2887-
from seleniumbase.core import detect_b_ver
2888-
28892900
if binary_location:
28902901
try:
28912902
major_chrome_version = (

seleniumbase/core/detect_b_ver.py

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,68 @@ def windows_browser_apps_to_cmd(*apps):
106106
return '%s -NoProfile "%s"' % (powershell, script)
107107

108108

109+
def get_binary_location(browser_type):
110+
cmd_mapping = {
111+
ChromeType.GOOGLE: {
112+
OSType.LINUX: linux_browser_apps_to_cmd(
113+
"google-chrome-stable",
114+
"google-chrome",
115+
"chrome",
116+
"chromium",
117+
"chromium-browser",
118+
"google-chrome-beta",
119+
"google-chrome-dev",
120+
"google-chrome-unstable",
121+
),
122+
OSType.MAC: r"/Applications/Google Chrome.app"
123+
r"/Contents/MacOS/Google Chrome",
124+
OSType.WIN: windows_browser_apps_to_cmd(
125+
r'(Get-Item -Path "$env:PROGRAMFILES\Google\Chrome'
126+
r'\Application\chrome.exe")',
127+
r'(Get-Item -Path "$env:PROGRAMFILES (x86)\Google\Chrome'
128+
r'\Application\chrome.exe")',
129+
r'(Get-Item -Path "$env:LOCALAPPDATA\Google\Chrome'
130+
r'\Application\chrome.exe")',
131+
),
132+
},
133+
ChromeType.MSEDGE: {
134+
OSType.LINUX: linux_browser_apps_to_cmd(
135+
"microsoft-edge-stable",
136+
"microsoft-edge",
137+
"microsoft-edge-beta",
138+
"microsoft-edge-dev",
139+
),
140+
OSType.MAC: r"/Applications/Microsoft Edge.app"
141+
r"/Contents/MacOS/Microsoft Edge",
142+
OSType.WIN: windows_browser_apps_to_cmd(
143+
# stable edge
144+
r'(Get-Item -Path "$env:PROGRAMFILES\Microsoft\Edge'
145+
r'\Application\msedge.exe")',
146+
r'(Get-Item -Path "$env:PROGRAMFILES (x86)\Microsoft'
147+
r'\Edge\Application\msedge.exe")',
148+
# beta edge
149+
r'(Get-Item -Path "$env:LOCALAPPDATA\Microsoft\Edge Beta'
150+
r'\Application\msedge.exe")',
151+
r'(Get-Item -Path "$env:PROGRAMFILES\Microsoft\Edge Beta'
152+
r'\Application\msedge.exe")',
153+
r'(Get-Item -Path "$env:PROGRAMFILES (x86)\Microsoft\Edge Beta'
154+
r'\Application\msedge.exe")',
155+
# dev edge
156+
r'(Get-Item -Path "$env:LOCALAPPDATA\Microsoft\Edge Dev'
157+
r'\Application\msedge.exe")',
158+
r'(Get-Item -Path "$env:PROGRAMFILES\Microsoft\Edge Dev'
159+
r'\Application\msedge.exe")',
160+
r'(Get-Item -Path "$env:PROGRAMFILES (x86)\Microsoft\Edge Dev'
161+
r'\Application\msedge.exe")',
162+
# canary edge
163+
r'(Get-Item -Path "$env:LOCALAPPDATA\Microsoft\Edge SxS'
164+
r'\Application\msedge.exe")',
165+
),
166+
},
167+
}
168+
return cmd_mapping[browser_type][os_name()]
169+
170+
109171
def get_browser_version_from_binary(binary_location):
110172
try:
111173
if binary_location.count(r"\ ") != binary_location.count(" "):
@@ -118,18 +180,19 @@ def get_browser_version_from_binary(binary_location):
118180
return None
119181

120182

121-
def get_browser_version_from_os(browser_type=None):
183+
def get_browser_version_from_os(browser_type):
122184
"""Return installed browser version."""
123185
cmd_mapping = {
124186
ChromeType.GOOGLE: {
125187
OSType.LINUX: linux_browser_apps_to_cmd(
126-
"google-chrome",
127188
"google-chrome-stable",
189+
"google-chrome",
128190
"chrome",
129191
"chromium",
192+
"chromium-browser",
130193
"google-chrome-beta",
131194
"google-chrome-dev",
132-
"chromium-browser",
195+
"google-chrome-unstable",
133196
),
134197
OSType.MAC: r"/Applications/Google\ Chrome.app"
135198
r"/Contents/MacOS/Google\ Chrome --version",
@@ -149,8 +212,8 @@ def get_browser_version_from_os(browser_type=None):
149212
},
150213
ChromeType.MSEDGE: {
151214
OSType.LINUX: linux_browser_apps_to_cmd(
152-
"microsoft-edge",
153215
"microsoft-edge-stable",
216+
"microsoft-edge",
154217
"microsoft-edge-beta",
155218
"microsoft-edge-dev",
156219
),

seleniumbase/fixtures/constants.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -360,23 +360,25 @@ class ValidBrowsers:
360360

361361
class ValidBinaries:
362362
valid_chrome_binaries_on_linux = [
363-
"google-chrome",
364363
"google-chrome-stable",
364+
"google-chrome",
365365
"chrome",
366366
"chromium",
367+
"chromium-browser",
367368
"google-chrome-beta",
368369
"google-chrome-dev",
369-
"chromium-browser",
370+
"google-chrome-unstable",
370371
]
371372
valid_edge_binaries_on_linux = [
372-
"microsoft-edge",
373373
"microsoft-edge-stable",
374+
"microsoft-edge",
374375
"microsoft-edge-beta",
375376
"microsoft-edge-dev",
376377
]
377378
valid_chrome_binaries_on_macos = [
378379
"Google Chrome",
379380
"Chromium",
381+
"Google Chrome for Testing",
380382
]
381383
valid_edge_binaries_on_macos = [
382384
"Microsoft Edge",

seleniumbase/undetected/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -506,13 +506,14 @@ def find_chrome_executable():
506506
if IS_POSIX:
507507
for item in os.environ.get("PATH").split(os.pathsep):
508508
for subitem in (
509-
"google-chrome",
510509
"google-chrome-stable",
510+
"google-chrome",
511511
"chrome",
512512
"chromium",
513+
"chromium-browser",
513514
"google-chrome-beta",
514515
"google-chrome-dev",
515-
"chromium-browser",
516+
"google-chrome-unstable",
516517
):
517518
candidates.add(os.sep.join((item, subitem)))
518519
if "darwin" in sys.platform:

0 commit comments

Comments
 (0)