node.js环境中使用playwright运行已打开的浏览器

查了好多文档大多数都是关于python的,自己终于根据文档找到了 node,js的写法,废话不多说直接上代码,官网链接Browser | Playwright

const { chromium } = require('playwright');
const axios = require('axios');
let headers
(async () => {

    const browser = await chromium.connectOverCDP("http://localhost:9001");

    const context = await browser.contexts()[0];

    const page = await context.pages()[0];

    await page.goto('https://www.baidu.com');

    // page.on('request', request => console.log('>>', request.method(), request.url()));

    // 拦截所有请求
    await page.route('**/*', async route => {
        headers = route.request().headers();
        // console.log('>>', headers.cookie)
        // delete headers['X-Secret'];
        // await route.continue({ headers });
    });

    await context.close();
    await browser.close();
})();

亲测可用!

还可以打开自己本地的谷歌浏览器

const { chromium } = require('playwright');

const axios = require('axios');

let headers
(async () => {


    const browser = await chromium.launch({
        headless: false,
        // 你本地谷歌浏览器的路径
        executablePath: "C:/Program Files/Google/Chrome/Application/chrome.exe"
    });
    // const browser = await chromium.connectOverCDP("http://localhost:9001");

    const context = await browser.newContext();

    const page = await context.newPage();

    await page.goto('https://www.bitget.fit/zh-CN/futures/susdt/SBTCSUSDT');

  
    // 拦截所有请求
    await page.route('**/*', async route => {
        headers = route.request().headers();
        // console.log('>>', headers.cookie)
        // delete headers['X-Secret'];
        // await route.continue({ headers });
    });

   
    await context.close();
    await browser.close();
})();

指定代理IP打开浏览器

 // 启动Playwright并指定代理

    const SERVE_PORT = 7157;
    const browser = await chromium.launch({
      args: ['--proxy-server=' + PROXYURL, "--lang=zh_CN", "--start-maximized"],
      headless: false,
      timeout: 0,
    });

    // const browser = await chromium.launch({ headless: false, slowMo: 50 });//模拟打开浏览器,设置有头模式,并通过slowMo属性减慢浏览器的每一步操作
    const context = await browser.newContext();//建立context
    const page = await context.newPage();//模拟打开一个浏览器的标签页

    await page.goto('https://www.bitget.fit/zh-CN/login');//模拟访问网站url

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值