-
Hi all, I'm putting this here since there is no nodriver discussions section. I'm trying to get the browser setup with an authenticated proxy but chrome does not support credentials via browser args. I found this, where the answer from Arkadiy Bolotov is interesting. There are the same cdp methods as in his post available in Network though they are deprecated for those in Fetch. Here is my current attempt I put together this afternoon, which does intercept the requests however no authRequired events are fired as the documentation suggests.
|
Beta Was this translation helpful? Give feedback.
Replies: 7 comments 31 replies
-
Hi Here 2 problems arises:
Solutions:
Example of working code based of of yours: class Scraper:
main_tab: uc.Tab
def __init__(self):
uc.loop().run_until_complete(self.run())
async def run(self):
browser = await uc.start(
browser_args=[f"--proxy-server={PROXY}"],
)
self.main_tab = await browser.get("draft:,")
self.main_tab.add_handler(uc.cdp.fetch.RequestPaused, self.req_paused)
self.main_tab.add_handler(
uc.cdp.fetch.AuthRequired, self.auth_challenge_handler
)
await self.main_tab.send(uc.cdp.fetch.enable(handle_auth_requests=True))
page = await browser.get("https://www.whatismyip.com/")
await asyncio.sleep(6000)
async def auth_challenge_handler(self, event: uc.cdp.fetch.AuthRequired):
# Split the credentials
# Respond to the authentication challenge
asyncio.create_task(
self.main_tab.send(
uc.cdp.fetch.continue_with_auth(
request_id=event.request_id,
auth_challenge_response=uc.cdp.fetch.AuthChallengeResponse(
response="ProvideCredentials",
username=USERNAME,
password=PASSWORD,
),
)
)
)
async def req_paused(self, event: uc.cdp.fetch.RequestPaused):
asyncio.create_task(
self.main_tab.send(
uc.cdp.fetch.continue_request(request_id=event.request_id)
)
)
if __name__ == "__main__":
Scraper() |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
Hi all, I have been trying to use a proxy with nodriver for a couple of days now, without success. I tried the suggested solutions but I keep getting this error in the chrome tab: In my case, in am trying to use smartproxy residential proxies. A snippet of working code with import requests
url = 'https://ip.smartproxy.com/json'
username = 'some_user'
password = 'some_password'
proxy = f"http://{username}:{password}@es.smartproxy.com:10000"
result = requests.get(url, proxies = {
'http': proxy,
'https': proxy
})
print(result.text) A working example with import seleniumwire.undetected_chromedriver as uc
# Proxy server details
PROXYUSERNAME = 'some_user'
PROXYPASSWORD = 'some_password'
PROXYPORT = '10000'
# define your proxy credentials
proxy_username = PROXYUSERNAME
proxy_password = PROXYPASSWORD
proxy_host = 'es.smartproxy.com'
proxy_port = PROXYPORT
# form the proxy address
proxy_address = f'http://{proxy_username}:{proxy_password}@{proxy_host}:{proxy_port}'
# add the proxy address to proxy options
proxy_options = {
'proxy': {
'http': proxy_address,
'https': proxy_address,
}
}
if __name__ == '__main__':
# set Chrome options
options = uc.ChromeOptions()
# run Chrome in headless mode
options.headless = True
options.add_argument('--ignore-certificate-errors')
# create a Chrome instance with the proxy options
driver = uc.Chrome(
seleniumwire_options=proxy_options,
options=options,
use_subprocess=False,
)
# visit the test URL to check your proxy IP
driver.get('https://httpbin.io/ip')
ip_address = driver.find_element(By.TAG_NAME, 'body').text
print(ip_address)
# close the browser
driver.quit() However, when using these credentials with |
Beta Was this translation helpful? Give feedback.
-
Hi, here's what I usually do for authentication-required SOCKS5 proxies with Chromium. I use GOST import subprocess
def start_gost_proxy(proxy, rport):
ip, port, user, password = proxy.split(":")
gost_proxy = f"socks5://{user}:{password}@{ip}:{port}"
gost_command = ["gost", "-L", f":{rport}", "-F", gost_proxy]
subprocess.Popen(gost_command, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
print(f"Started GOST proxy with {gost_proxy} on port {rport}")
async def login(email, password):
start_gost_proxy("YourProxyIP:YourProxyPort:Username:Password", 2000)
browser = await start(headless=False, lang="en-US", browser_args=["--proxy-server=socks5://localhost:2000"]) |
Beta Was this translation helpful? Give feedback.
-
Proxy with auth solved in latest version, you can also use per window/tab another proxy |
Beta Was this translation helpful? Give feedback.
-
I'm trying to use a SOCKS5 proxy with authentication in Codeasync def init_browser():
browser = await uc.start(
headless=False,
browser_args=[
"--start-maximized"
]
)
proxied_tab = await browser.create_context(
proxy_server = "socks5://user:[email protected]:8005",
proxy_bypass_list = ["localhost"]
)
await proxied_tab.get('https://whatismyip.com')
return browser, proxied_tab
async def main():
browser, main_tab = await init_browser()
await browser.wait(3600)
if __name__ == "__main__":
uc.loop().run_until_complete(main())
Error
Does anyone know how to properly set a SOCKS5 proxy with authentication in nodriver? Any help or advice is appreciated. |
Beta Was this translation helpful? Give feedback.
-
We cannot get a proxy with auth working directly in the browser, because auth is not supported in the URL and you need to pause a request to handle it. The issue we got was that every other requests were paused when we didn't want them paused, so we could not do anything. The way we made it work was by using gost to start a local proxy that handles auth for us, and then connecting the browser to the local proxy without auth: # Somewhere before starting the browser
# In your case, url = "socks5://user:[email protected]:8005"
gost_command = ["gost", "-L", f":{port}", "-F", url]
subprocess.Popen(gost_command, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
# When starting the browser
browser_args = [
f"--proxy-server=socks5://localhost:{port}",
# ...
] |
Beta Was this translation helpful? Give feedback.
Hi
Here 2 problems arises:
Solutions:
fire-and-forget
method e.g. do not await for sending response onreq_paused
orauth_required
. Just wrap the call intoasyncio.create_task
Example of working code based of of yours: