> 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'
},
```

输入您的 `服务器` 值，具体取决于您选择的 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 %}

你还可以使用 **附加参数** 搭配住宅代理和移动代理来设置地理位置（国家/地区、州、省、市等）并控制会话。查看 [住宅代理文档](/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 计费订阅**

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

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

**按流量计费订阅**

该端口 `8001` 会随机选择一个 IP 地址，但在会话期间保持一致。或者，你可以通过使用端口 [轮换代理 IP](/products/cn/proxies/datacenter-proxies/ip-control.md) 来在每个请求中更换 IP `8000`.

如果你想使用特定的 [地理位置](/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` （来自你的 [代理列表](/products/cn/proxies/dedicated-datacenter-proxies/enterprise/proxy-lists.md))

**端口：** `60000` (HTTP), `1180` (SOCKS5)

**用户名：** `username`

**密码：** `password`

对于企业版独享数据中心代理（通过销售团队购买），你需要从已获取的 [代理列表](/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 仪表盘购买），端口表示从已获取的 [代理列表](/products/cn/proxies/dedicated-datacenter-proxies/self-service/proxy-list.md)的 IP 地址的顺序编号。如果你希望每个请求都获得不同的 IP，请将端口改为 `8000` 以启用 [代理轮换](/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` 将始终选择你 [代理列表](/products/cn/proxies/isp-proxies/proxy-list.md)的 IP 地址的顺序编号。如果你希望每个请求都获得不同的 IP，请将端口改为 `8000` 以启用 [代理轮换](/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 地址的端口 [代理列表](/products/cn/proxies/dedicated-isp-proxies/self-service/proxy-list.md)，你可以在 Oxylabs 控制面板中找到。 `8001` 端口将为所有请求选取列表中的第一个 IP 地址。使用端口 `8000` 可自动进行 [代理 IP 轮换](/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();
    }
})();
```

就是这样！您已成功将 Oxylabs 代理与 Playwright 集成。如果您想了解更多关于使用 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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://developers.oxylabs.io/integrations/cn/proxy-integrations/playwright.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
