Skip to content

Commit b80614b

Browse files
committed
Fix issue with proxies in UC Mode
1 parent 2ccb7b5 commit b80614b

File tree

1 file changed

+32
-10
lines changed

1 file changed

+32
-10
lines changed

seleniumbase/core/browser_launcher.py

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,16 +96,35 @@ def make_driver_executable_if_not(driver_path):
9696
make_executable(driver_path)
9797

9898

99-
def requests_get(url):
99+
def requests_get(url, proxy_string=None):
100100
import requests
101101

102+
protocol = "http"
103+
if proxy_string:
104+
if proxy_string.endswith(":443"):
105+
protocol = "https"
106+
elif "socks4" in proxy_string:
107+
protocol = "socks4"
108+
elif "socks5" in proxy_string:
109+
protocol = "socks5"
102110
response = None
103-
try:
104-
response = requests.get(url)
105-
except Exception:
106-
# Prevent SSLCertVerificationError / CERTIFICATE_VERIFY_FAILED
107-
url = url.replace("https://", "http://")
108-
response = requests.get(url)
111+
if proxy_string:
112+
proxies = {protocol: proxy_string}
113+
try:
114+
response = requests.get(url, proxies=proxies)
115+
except Exception:
116+
# Prevent SSLCertVerificationError / CERTIFICATE_VERIFY_FAILED
117+
url = url.replace("https://", "http://")
118+
time.sleep(0.04)
119+
response = requests.get(url, proxies=proxies)
120+
else:
121+
try:
122+
response = requests.get(url)
123+
except Exception:
124+
# Prevent SSLCertVerificationError / CERTIFICATE_VERIFY_FAILED
125+
url = url.replace("https://", "http://")
126+
time.sleep(0.04)
127+
response = requests.get(url)
109128
return response
110129

111130

@@ -178,6 +197,7 @@ def find_edgedriver_version_to_use(use_version, driver_version):
178197
def has_cf(text):
179198
if (
180199
"<title>Just a moment...</title>" in text
200+
or "<title>403 Forbidden</title>" in text
181201
or 'id="challenge-error-text"' in text
182202
or 'action="/?__cf_chl_f_tk' in text
183203
or 'id="challenge-form"' in text
@@ -187,10 +207,10 @@ def has_cf(text):
187207
return False
188208

189209

190-
def uc_special_open_if_cf(driver, url):
210+
def uc_special_open_if_cf(driver, url, proxy_string=None):
191211
if (
192212
(url.startswith("http:") or url.startswith("https:"))
193-
and has_cf(requests_get(url).text)
213+
and has_cf(requests_get(url, proxy_string).text)
194214
):
195215
with driver:
196216
time.sleep(0.25)
@@ -3405,7 +3425,9 @@ def get_local_driver(
34053425
)
34063426
driver.open = driver.get # Save copy of original
34073427
if uc_activated:
3408-
driver.get = lambda url: uc_special_open_if_cf(driver, url)
3428+
driver.get = lambda url: uc_special_open_if_cf(
3429+
driver, url, proxy_string
3430+
)
34093431
driver.uc_open = lambda url: uc_open(driver, url)
34103432
driver.uc_open_with_tab = (
34113433
lambda url: uc_open_with_tab(driver, url)

0 commit comments

Comments
 (0)