> For the complete documentation index, see [llms.txt](https://docs.ipdunar.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.ipdunar.com/zh/ji-cheng-zhi-nan/pa-chong-gong-ju/playwright.md).

# Playwright

Playwright 对代理有一等支持——直接把凭证传给 `launch()` 或 `new_context()`，无需扩展技巧。

## Python

```python
from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.launch(proxy={
        "server":   "http://gate.ipdunar.com:7777",
        "username": "ipdunar_s1a2b3c4d5e-type-res-region-us",
        "password": "PASSWORD",
    })
    page = browser.new_page()
    page.goto("https://ipv4.icanhazip.com")
    print(page.content())
    browser.close()
```

SOCKS5 用 `"server": "socks5://gate.ipdunar.com:7777"`，DNS 由代理解析。

## Node.js

```js
import { chromium } from "playwright";

const browser = await chromium.launch({
  proxy: {
    server:   "http://gate.ipdunar.com:7777",
    username: "ipdunar_s1a2b3c4d5e-type-res-region-us",
    password: "PASSWORD",
  },
});
const page = await browser.newPage();
await page.goto("https://ipv4.icanhazip.com");
console.log(await page.content());
await browser.close();
```

## 按 Context 配代理

不同浏览器 Context 用不同代理——适合并发会话：

```python
ctx_us = browser.new_context(proxy={"server": "http://gate.ipdunar.com:7777",
                                    "username": "ipdunar_s1a2b3c4d5e-type-res-region-us",
                                    "password": "PASSWORD"})
ctx_de = browser.new_context(proxy={"server": "http://gate.ipdunar.com:7777",
                                    "username": "ipdunar_s1a2b3c4d5e-type-res-region-de",
                                    "password": "PASSWORD"})
```

浏览器进程共享，Context 之间彼此隔离，像独立的 profile。

## ISP

```python
browser = p.chromium.launch(proxy={
    "server":   "http://198.51.100.42:8000",
    "username": "ipdunar_s1a2b3c4d5e",
    "password": "PASSWORD",
})
```

## Playwright 中轮换会话

每个 Context 生成新的子账号字符串，带独立 `session-XXX`：

```python
import random, string
def session_user(country="us"):
    sid = "".join(random.choices(string.ascii_lowercase + string.digits, k=8))
    return f"ipdunar_s1a2b3c4d5e-type-res-region-{country}-session-{sid}-sesstime-10"

ctx = browser.new_context(proxy={
    "server": "http://gate.ipdunar.com:7777",
    "username": session_user(),
    "password": "PASSWORD",
})
```

## 验证

```python
ip = page.evaluate("fetch('https://ipv4.icanhazip.com').then(r => r.text())")
print(ip)
```

## 常见陷阱

* **代理认证无需 headless 特殊参数** — Playwright 在 headful/headless 下都原生支持。
* **WebRTC 泄露** — Playwright 默认不通过 WebRTC 暴露本机 IP，若目标会嗅探请用 `browserleaks.com/webrtc` 复核。
* **绕过清单** — 用 `proxy.bypass = "localhost,127.0.0.1"` 避免把内网目标也代理出去。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.ipdunar.com/zh/ji-cheng-zhi-nan/pa-chong-gong-ju/playwright.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
