Skip to content

Playwright

什么是Playwright?

Playwright由 Microsoft 构建,是一个 Node.js 库,透过单一 API,可以自动化在 Chromium、Firefox 和 WebKit 上运行的网站和 Web 应用程式。编写 JavaScript 程式码的开发人员可以使用这些 API 来建立新的浏览器页面、导航到 URL,然后与页面上的元素互动。此外,由于 Microsoft Edge 建置在开源 Chromium Web 平台上,Playwright 还可以自动化 Microsoft Edge。

下面为您介绍如何将Socks5与Playwright集成。

1

安装Playwright

首先使用 npm、yarn 或 pnpm 安装 Playwright。或者,您也可以使用 VS Code 扩充功能开始并执行测试。

点击此处查看詳細安裝教程

2

在Playwright中填入代理值

服务器: proxy-as.socks5.io:3000

代理账户: socks5-customer-USERNAME

代理密码: PASSWORD

代码示例:

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