Skip to content

Commit be477fa

Browse files
committed
Prevent sites from detecting SeleniumBase ("--uc" mode)
1 parent 618f12b commit be477fa

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

seleniumbase/core/browser_launcher.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,32 @@ def get_uc_driver_version():
168168
return uc_driver_version
169169

170170

171+
def has_cf(text):
172+
if (
173+
"<title>Just a moment...</title>" in text
174+
or 'id="challenge-error-text"' in text
175+
or 'action="/?__cf_chl_f_tk' in text
176+
or 'id="challenge-form"' in text
177+
or "window._cf_chl_opt" in text
178+
):
179+
return True
180+
return False
181+
182+
183+
def uc_open(driver, url):
184+
if (
185+
(url.startswith("http:") or url.startswith("https:"))
186+
and has_cf(requests_get(url).text)
187+
):
188+
driver.execute_script('window.open("%s","_blank");' % url)
189+
time.sleep(2.75)
190+
driver.execute_script("window.close();")
191+
driver.switch_to.window(driver.window_handles[-1])
192+
else:
193+
driver.open(url) # The original one
194+
return None
195+
196+
171197
def edgedriver_on_path():
172198
return os.path.exists(LOCAL_EDGEDRIVER)
173199

@@ -742,6 +768,9 @@ def _set_chrome_options(
742768
chrome_options.add_argument("--use-gl=swiftshader")
743769
elif not is_using_uc(undetectable, browser_name):
744770
chrome_options.add_argument("--disable-gpu")
771+
if not IS_LINUX and is_using_uc(undetectable, browser_name):
772+
chrome_options.add_argument("--disable-dev-shm-usage")
773+
chrome_options.add_argument("--disable-application-cache")
745774
if IS_LINUX:
746775
chrome_options.add_argument("--disable-dev-shm-usage")
747776
if is_using_uc(undetectable, browser_name):
@@ -3008,6 +3037,7 @@ def get_local_driver(
30083037
or not IS_LINUX
30093038
or is_using_uc(undetectable, browser_name)
30103039
):
3040+
uc_activated = False
30113041
try:
30123042
if (
30133043
os.path.exists(LOCAL_CHROMEDRIVER)
@@ -3051,6 +3081,7 @@ def get_local_driver(
30513081
version_main=uc_chrome_version,
30523082
use_subprocess=True, # Always!
30533083
)
3084+
uc_activated = True
30543085
except URLError as e:
30553086
if cert in e.args[0] and IS_MAC:
30563087
mac_certificate_error = True
@@ -3082,6 +3113,7 @@ def get_local_driver(
30823113
version_main=uc_chrome_version,
30833114
use_subprocess=True, # Always!
30843115
)
3116+
uc_activated = True
30853117
else:
30863118
service = ChromeService(
30873119
executable_path=LOCAL_CHROMEDRIVER,
@@ -3246,6 +3278,9 @@ def get_local_driver(
32463278
service_args=["--disable-build-check"],
32473279
options=chrome_options,
32483280
)
3281+
driver.open = driver.get # Save copy of original
3282+
if uc_activated:
3283+
driver.get = lambda url: uc_open(driver, url)
32493284
return driver
32503285
else: # Running headless on Linux (and not using --uc)
32513286
try:

0 commit comments

Comments
 (0)