Skip to content

Commit 71e1cc8

Browse files
authored
Merge pull request #282 from autoscrape-labs/docs/private-proxy-interception
Docs/private proxy interception
2 parents 91c1b74 + 99e0f9f commit 71e1cc8

File tree

3 files changed

+86
-16
lines changed

3 files changed

+86
-16
lines changed

public/docs/features.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,6 +1331,40 @@ asyncio.run(iframe_interaction())
13311331

13321332
Intercept and modify network requests before they're sent:
13331333

1334+
!!! note "Private proxy + request interception (Fetch)"
1335+
When using a private/authenticated proxy, Pydoll enables Fetch at the Browser level to handle the proxy authentication challenge during the first navigation and may change its state. To avoid domain conflicts with your Tab-level interception, enable Fetch at the Tab level only after the first navigation completes.
1336+
1337+
Recommended pattern:
1338+
1339+
```python
1340+
import asyncio
1341+
from pydoll.browser.chromium import Chrome
1342+
from pydoll.browser.options import ChromiumOptions
1343+
1344+
async def main():
1345+
options = ChromiumOptions()
1346+
options.add_argument('--proxy-server=username:password@host:port')
1347+
1348+
async with Chrome(options=options) as browser:
1349+
tab = await browser.start()
1350+
1351+
# 1) Trigger proxy auth at Browser level first
1352+
await tab.go_to('https://example.com')
1353+
1354+
# 2) Then enable Tab-level Fetch interception safely
1355+
await tab.enable_fetch_events()
1356+
1357+
async def on_paused(event):
1358+
await tab.continue_request(event['params']['requestId'])
1359+
1360+
await tab.on('Fetch.requestPaused', on_paused)
1361+
1362+
# 3) Proceed with your navigations/interception
1363+
await tab.go_to('https://example.com/next')
1364+
1365+
asyncio.run(main())
1366+
```
1367+
13341368
### Basic Request Modification
13351369

13361370
```python

public/docs/zh/features.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,6 +1346,40 @@ asyncio.run(iframe_interaction())
13461346

13471347
在网络请求发送之前拦截并修改它们:
13481348

1349+
!!! 信息 "私有代理 + 请求拦截(Fetch)"
1350+
当使用带认证的私有代理时,Pydoll 会在浏览器(Browser)级别启用 Fetch 以处理首次导航过程中的代理认证挑战,其状态变化可能会影响页面(Tab)级别的拦截。为避免域冲突,请在首次导航完成后再在 Tab 级别启用 Fetch 拦截。
1351+
1352+
推荐用法:
1353+
1354+
```python
1355+
import asyncio
1356+
from pydoll.browser.chromium import Chrome
1357+
from pydoll.browser.options import ChromiumOptions
1358+
1359+
async def main():
1360+
options = ChromiumOptions()
1361+
options.add_argument('--proxy-server=username:password@host:port')
1362+
1363+
async with Chrome(options=options) as browser:
1364+
tab = await browser.start()
1365+
1366+
# 1) 先进行一次导航,触发浏览器级别的代理认证
1367+
await tab.go_to('https://example.com')
1368+
1369+
# 2) 然后在 Tab 级别安全地启用 Fetch 拦截
1370+
await tab.enable_fetch_events()
1371+
1372+
async def on_paused(event):
1373+
await tab.continue_request(event['params']['requestId'])
1374+
1375+
await tab.on('Fetch.requestPaused', on_paused)
1376+
1377+
# 3) 继续你的导航/拦截逻辑
1378+
await tab.go_to('https://example.com/next')
1379+
1380+
asyncio.run(main())
1381+
```
1382+
13491383
### 简单请求修改例子
13501384

13511385
```python

pydoll/browser/chromium/base.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -656,22 +656,24 @@ async def _configure_proxy(
656656
self, private_proxy: bool, proxy_credentials: tuple[Optional[str], Optional[str]]
657657
):
658658
"""Setup proxy authentication handling if needed."""
659-
if private_proxy:
660-
await self.enable_fetch_events(handle_auth_requests=True)
661-
await self.on(
662-
FetchEvent.REQUEST_PAUSED,
663-
self._continue_request_callback,
664-
temporary=True,
665-
)
666-
await self.on(
667-
FetchEvent.AUTH_REQUIRED,
668-
partial(
669-
self._continue_request_with_auth_callback,
670-
proxy_username=proxy_credentials[0],
671-
proxy_password=proxy_credentials[1],
672-
),
673-
temporary=True,
674-
)
659+
if not private_proxy:
660+
return
661+
662+
await self.enable_fetch_events(handle_auth_requests=True)
663+
await self.on(
664+
FetchEvent.REQUEST_PAUSED,
665+
self._continue_request_callback,
666+
temporary=True,
667+
)
668+
await self.on(
669+
FetchEvent.AUTH_REQUIRED,
670+
partial(
671+
self._continue_request_with_auth_callback,
672+
proxy_username=proxy_credentials[0],
673+
proxy_password=proxy_credentials[1],
674+
),
675+
temporary=True,
676+
)
675677

676678
@staticmethod
677679
def _is_valid_tab(target: TargetInfo) -> bool:

0 commit comments

Comments
 (0)