Skip to content

Commit d8bf665

Browse files
committed
Add proxy section to the Stealthy Playwright ReadMe
1 parent 7937f7e commit d8bf665

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

examples/cdp_mode/playwright/ReadMe.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,73 @@ with sync_playwright() as p:
139139
sb.sleep(3)
140140
```
141141

142+
--------
143+
144+
### 🎭 Proxy with auth in <b translate="no">Stealthy Playwright Mode</b>:
145+
146+
To use an authenticated proxy in Stealthy Playwright Mode, **do these two things**:<br />**1.** Set the`proxy` arg when launching Chrome.
147+
-- Eg: `sb_cdp.Chrome(proxy="USER:PASS@IP:PORT")` or `cdp_driver.start_async("USER:PASS@IP:PORT")`.<br />**2.** Open the URL with SeleniumBase **before** using `endpoint_url` to connect to the browser with Playwright.
148+
149+
⚠️ If any trouble with the above, set `use_chromium=True` so that you can use the base Chromium browser, which still allows extensions, unlike regular branded Chrome, which removed the `--load-extension` command-line switch. (*An extension is used to set the auth for the proxy, which is needed when CDP can't set the proxy alone, such as for navigation after the initial page load*).
150+
151+
In the sync format, use `sb.open(url)` to open the url before connecting Playwright:
152+
```python
153+
sb = sb_cdp.Chrome(use_chromium=True, proxy="user:pass@server:port")
154+
sb.open(url)
155+
endpoint_url = sb.get_endpoint_url()
156+
# ...
157+
```
158+
159+
In the async format, use, `driver.get(url)` to open the url before connecting Playwright:
160+
```python
161+
driver = await cdp_driver.start_async(use_chromium=True, proxy="user:pass@server:port")
162+
await driver.get(url)
163+
endpoint_url = driver.get_endpoint_url()
164+
# ...
165+
```
166+
167+
Here's an example of using an authenticated proxy with Stealthy Playwright Mode:<br />(The URL is opened before attaching Playwright so that proxy settings take effect)
168+
```python
169+
from playwright.sync_api import sync_playwright
170+
from seleniumbase import sb_cdp
171+
172+
sb = sb_cdp.Chrome(use_chromium=True, proxy="user:pass@server:port")
173+
sb.open(url)
174+
endpoint_url = sb.get_endpoint_url()
175+
176+
with sync_playwright() as p:
177+
browser = p.chromium.connect_over_cdp(endpoint_url)
178+
context = browser.contexts[0]
179+
page = context.pages[0]
180+
# ...
181+
```
182+
(Fill in the `url` and the `proxy` details to complete the script.)
183+
184+
Here's the same thing for the `async` format:
185+
```python
186+
import asyncio
187+
from playwright.async_api import async_playwright
188+
from seleniumbase import cdp_driver
189+
190+
async def main():
191+
driver = await cdp_driver.start_async(use_chromium=True, proxy="user:pass@server:port")
192+
await driver.get(url)
193+
endpoint_url = driver.get_endpoint_url()
194+
195+
async with async_playwright() as p:
196+
browser = await p.chromium.connect_over_cdp(endpoint_url)
197+
context = browser.contexts[0]
198+
page = context.pages[0]
199+
# ...
200+
201+
if __name__ == "__main__":
202+
loop = asyncio.new_event_loop()
203+
loop.run_until_complete(main())
204+
```
205+
(Fill in the `url` and the `proxy` details to complete the script.)
206+
207+
--------
208+
142209
For more examples, see [examples/cdp_mode/playwright](https://github.com/seleniumbase/SeleniumBase/tree/master/examples/cdp_mode/playwright).
143210

144211
--------

0 commit comments

Comments
 (0)