> For the complete documentation index, see [llms.txt](https://developers.oxylabs.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developers.oxylabs.io/integrations/cn/proxy-integrations/playwright.md).

# Playwright

将 Playwright 与 Oxylabs 代理集成，用于浏览器自动化——带代码示例的 Node.js 设置指南。

[**Playwright**](https://playwright.dev/) 是一个 Node.js 库，可让你使用同一 API 编写脚本并自动化浏览器，例如 Chrome、Firefox 和 Safari。它是一个适合自动化和测试用途的便捷框架。

观看我们的视频指南，或按照以下步骤将 Playwright 与以下 Oxylabs 代理集成：

* [**住宅和移动代理**](#residential-and-mobile-proxies)
* [**数据中心代理**](#datacenter-proxies)
* [**独享数据中心代理（企业版）**](#dedicated-datacenter-proxies-enterprise)
* [**独享数据中心代理（自助版）**](#dedicated-datacenter-proxies-self-service)
* [**ISP代理**](#isp-proxies)
* [**独享ISP代理**](#dedicated-isp-proxies)

{% embed url="<https://youtu.be/KIsQ7ggB5DU>" %}

### **步骤 1**

安装 [Node.js](https://nodejs.org/en/download) 以及你选择的代码编辑器。（如果你已经有了，可跳过此步骤）

### **步骤 2**

创建一个 Node.js 项目并安装所需的包。

### **步骤 3**

在你的 Playwright 脚本中，填入 `'proxy'` 的值，如下所示：

```javascript
proxy: {
    server: 'protocol-host-port',
    username: 'USERNAME',
    password: 'PASSWORD'
},
```

输入你的 `server` 值，具体取决于你选择的 Oxylabs 代理类型：

* [住宅和移动代理](#residential-and-mobile-proxies)
* [数据中心代理](#datacenter-proxies)
* [独享数据中心代理（企业版）](#dedicated-datacenter-proxies-enterprise)
* [独享数据中心代理（自助版）](#dedicated-datacenter-proxies-self-service)
* [ISP代理](#isp-proxies)
* [独享ISP代理](#dedicated-isp-proxies)

#### 住宅和移动代理

```javascript
server: 'http://pr.oxylabs.io:7777' //protocol-host-port
```

**协议：** `http`, `https`，或 `socks5h`

**主机：** `pr.oxylabs.io`

**端口：** `7777`

**用户名：** `customer-USERNAME`

**密码：** `PASSWORD`

{% hint style="warning" %}
**注意：** 别忘了添加 `customer-` 前缀到您的用户名中。
{% endhint %}

你还可以使用 **附加参数** 与住宅和移动代理一起使用，以设置地理位置（国家、州、城市等）并控制会话。请查看 [住宅代理文档](https://developers.oxylabs.io/products/cn/proxies/residential-proxies.md) 了解更多。

#### 数据中心代理

```javascript
server: 'http://dc.oxylabs.io:8001' //protocol-host-port
```

**协议：** `http`, `https`，或 `socks5h`

**主机：** `dc.oxylabs.io`

**端口：** `8001`

**用户名：** `user-username`

**密码：** `password`

{% hint style="warning" %}
**注意：** 别忘了添加 `user-` 前缀到您的用户名中。
{% endhint %}

**按 IP 计费订阅**

端口是分配给列表中 IP 地址的顺序编号 [代理列表](https://developers.oxylabs.io/products/cn/proxies/datacenter-proxies/proxy-list.md)的 IP 地址的顺序编号。因此，端口 `8001` 会使用你列表中的第一个 IP 地址。你还可以通过将端口设置为 [代理 IP 轮换](https://developers.oxylabs.io/products/cn/proxies/datacenter-proxies/ip-control.md) 来启用 `8000`.

此外，你还可以指定 [地理位置](https://developers.oxylabs.io/products/cn/proxies/datacenter-proxies/select-country.md)，例如美国，方法是将 **代理用户名** 设置为 `user-USERNAME-country-US`.

**按流量计费订阅**

端口 `8001` 会随机选取一个 IP 地址，但在会话期间保持一致。或者，你可以 [轮换代理 IP](https://developers.oxylabs.io/products/cn/proxies/datacenter-proxies/ip-control.md) ，通过使用端口 `8000`.

如果你想使用特定的 [地理位置](https://developers.oxylabs.io/products/cn/proxies/datacenter-proxies/select-country.md)，例如美国，请将代理用户名设置为 `user-USERNAME-country-US`.

#### 独享数据中心代理（企业版）

```javascript
server: 'http://1.2.3.4:60000' //protocol-host-port
```

**协议：** `http`，或 `socks5h`

**主机：** `1.2.3.4` （来自你的 [代理列表](https://developers.oxylabs.io/products/cn/proxies/dedicated-datacenter-proxies/enterprise/proxy-lists.md))

**端口：** `60000` （HTTP）， `1180` （SOCKS5）

**用户名：** `username`

**密码：** `password`

对于企业独享数据中心代理（通过销售团队购买），你必须从已获得的 [代理列表](https://developers.oxylabs.io/products/cn/proxies/dedicated-datacenter-proxies/enterprise/proxy-lists.md).

#### 独享数据中心代理（自助版）

```javascript
server: 'http://ddc.oxylabs.io:8001' //protocol-host-port
```

**协议：** `http`, `https`，或 `socks5h`

**主机：** `ddc.oxylabs.io`

**端口：** `8001`

**用户名：** `user-username`

**密码：** `password`

{% hint style="warning" %}
**注意：** 别忘了添加 `user-` 前缀到您的用户名中。
{% endhint %}

对于自助服务独享数据中心代理（通过 Oxylabs 控制面板购买），端口表示来自已获得的 IP 地址的顺序编号 [代理列表](https://developers.oxylabs.io/products/cn/proxies/dedicated-datacenter-proxies/self-service/proxy-list.md)中某个 IP 地址的顺序编号。如果你想让每个请求使用不同的 IP，请将端口改为 `8000` 以启用 [代理轮换](https://developers.oxylabs.io/products/cn/proxies/dedicated-datacenter-proxies/self-service/proxy-rotation.md).

#### ISP代理

```javascript
server: 'http://isp.oxylabs.io:8001' //protocol-host-port
```

**协议：** `http`, `https`，或 `socks5h`

**主机：** `isp.oxylabs.io`

**端口：** `8001`

**用户名：** `user-username`

**密码：** `password`

{% hint style="warning" %}
**注意：** 别忘了添加 `user-` 前缀到您的用户名中。
{% endhint %}

对于 ISP 代理，端口会分配给特定的 IP 地址及其位置。因此，端口 `8001` 将始终选取你 [代理列表](https://developers.oxylabs.io/products/cn/proxies/isp-proxies/proxy-list.md)中某个 IP 地址的顺序编号。如果你想让每个请求使用不同的 IP，请将端口改为 `8000` 以启用 [代理轮换](https://developers.oxylabs.io/products/cn/proxies/isp-proxies/proxy-rotation.md).

#### 独享ISP代理

```javascript
server: 'http://disp.oxylabs.io:7777' //protocol-host-port
```

**协议：** `http`, `https`，或 `socks5h`

**主机：** `disp.oxylabs.io`

**端口：** `8001`

**用户名：** `user-username`

**密码：** `password`

{% hint style="warning" %}
**注意：** 别忘了添加 `user-` 前缀到您的用户名中。
{% endhint %}

你需要选择分配给已购买的 IP 地址的端口， [代理列表](https://developers.oxylabs.io/products/cn/proxies/dedicated-isp-proxies/self-service/proxy-list.md)，你可以在 Oxylabs 控制面板中找到。 `8001` 端口会为所有请求从你的列表中选取第一个 IP 地址。使用端口 `8000` 用于自动 [代理 IP 轮换](https://developers.oxylabs.io/products/cn/proxies/dedicated-isp-proxies/self-service/proxy-rotation.md).

### 完整代码示例

```javascript
const playwright = require('playwright');
(async () => {
    for (const browserType of ['chromium', 'firefox', 'webkit']) {
        const browser = await playwright[browserType].launch({
            headless: false,
            proxy: {
                server: 'http://pr.oxylabs.io:7777',
                username: 'USERNAME',
                password: 'PASSWORD'
            },
        });
        const context = await browser.newContext();
        const page = await context.newPage();
        await page.goto('https://ip.oxylabs.io');
        await page.screenshot({ path: `${browserType}.png` });
        await browser.close();
    }
})();
```

如果你想进一步了解使用 Playwright 进行网页抓取，请阅读我们的 [博客文章](https://oxylabs.io/blog/playwright-web-scraping).

查找原始的 Playwright 集成博客文章 [这里](https://oxylabs.io/resources/integrations/playwright).


---

# 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://developers.oxylabs.io/integrations/cn/proxy-integrations/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.
