如何在Multilogin 6 中使用Puppeteer执行浏览器自动化
Puppeteer 是一个 Node.js 库,它使用基于 Chromium 的浏览器自动执行流程。它与 Chrome DevTools 协议配合使用,允许您在使用带有屏蔽指纹的Mimic浏览器时构建网络爬虫并与网站交互。
步骤1:定义应用程序监听端口
Define ports MLA CN
以下是在app.properties
文件中预定义默认监听端口的方法:
- 打开Multilogin
- 前往“我的帐户”
- 点击“打开日志目录” – 这将打开文件夹
/.multiloginapp.com/logs
- 导航至一个文件夹至
/.multiloginapp.com
- 使用任何文本编辑器打开
app.properties
- 添加新行来指定端口号:
multiloginapp.port=35000
- 保存更改
确保端口号介于 10000 和 49151 之间。
您还可以在.multiloginapp.com
文件夹中找到app.properties
文件:
- Windows :
C:\Users\%username%\.multiloginapp.com
- Linux :
/home/%username%/.multiloginapp.com
名%/.multiloginapp.com - macOS :
/Users/%username%/.multiloginapp.com
.multiloginapp.com
该文件夹可能被隐藏,具体取决于您的操作系统设置。要在 Mac 设备上显示此文件夹,您可以使用以下键盘快捷键:
-
Cmd + Shift + H
– 显示当前用户的文件夹 -
Cmd + Shift + .
(句点)– 显示隐藏文件夹和文件

第 2 步:设置Puppeteer
安装 Node.js 和 npm
开始之前,请确保您的计算机上安装了 Node.js 和 npm 包管理器。如果尚未安装,请从Node.js 网站下载。
要检查您的 Node.js 和 npm 版本,请打开终端并运行: node -v || node -v && npm -v
。
- Node.js 的最新版本已经默认包含 npm。
- 您还可以使用yarn进行 Node.js 包管理。
创建一个新的 npm 项目
导航到您想要的项目目录并运行npm init -y
。
此命令将生成一个package.json
文件。 -y
标志跳过问卷调查并使用默认值设置项目。
安装Puppeteer -Core
要安装Puppeteer -core,请运行npm install [email protected] --save
。
创建自动化脚本
现在,创建一个.js
文件并添加您的自动化代码。这是一个基本示例,您可以稍后修改它以满足您的自动化需求!
const puppeteer = require('puppeteer-core');
const http = require('http');
async function startProfile(){
//Replace profileId value with existing browser profile ID.
let profileId = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx';
let mlaPort = 35000;
/*Send GET request to start the browser profile by profileId.
Returns web socket as response which should be passed to puppeteer.connect*/
http.get(`http://127.0.0.1:${mlaPort}/api/v1/profile/start?automation=true&puppeteer=true&profileId=${profileId}`, (resp) => {
let data = '';
let ws = '';
//Receive response data by chunks
resp.on('data', (chunk) => {
data += chunk;
});
/*The whole response data has been received. Handling JSON Parse errors,
verifying if ws is an object and contains the 'value' parameter.*/
resp.on('end', () => {
let ws;
try {
ws = JSON.parse(data);
} catch(err) {
console.log(err);
}
if (typeof ws === 'object' && ws.hasOwnProperty('value')) {
console.log(`Browser websocket endpoint: ${ws.value}`);
run(ws.value);
}
});
}).on("error", (err) => {
console.log(err.message);
});
}
async function run(ws) {
try{
//Connecting Puppeteer with Mimic instance and performing simple automation.
const browser = await puppeteer.connect({browserWSEndpoint: ws, defaultViewport:null});
const page = await browser.newPage();
await page.goto('https://multilogin.com');
await page.screenshot({ path: `/home/${process.env.USER}/Desktop/multiloginScreenshot.png` });
await browser.close();
} catch(err){
console.log(err.message);
}
}
startProfile();
启动文件
通过终端启动.js
文件以使用nodejs example.js
启动自动化脚本。