puppeteer(六)启动参数——浏览器扩展应用的添加及应用

前言

最近再做浏览器的自动化,首页是定制化的,是通过extension(扩展)实现的,由于通过puppeteer默认是以无参(即首次以干净的环境)运行的,导致登录页无法正常显示,首先想当然是直接找扩展crx插件,结果是以失败告终,开始以为是调的方式方法不正确,最后询问几个大虾,都一致认为是在userdata下的扩展。如下两张图的对比

puppeteer(六)启动参数——浏览器扩展应用的添加及应用

puppeteer(六)启动参数——浏览器扩展应用的添加及应用

代码

const puppeteer = require('puppeteer'); //导入模块
const path = require('path');

(async () => {
  let username = 'tester02'; //需求根据不同用户修改

  const ext='C:/Users/tester02/AppData/Local/AllMobilize/Enterplorer/User Data/Default/Extensions';
  const exepath = 'C:/Users/' + username + '/AppData/Local/Axxx/Enterplorer/Application/enterplorer.exe';
  const userData = 'C:/Users/' + username + '/AppData/Local/Axxx/Enterplorer/User Data';
  const newtabEx = path.join(ext,"milhmifoljmifighlanebdlekdjhbibd/1.3.2_0/");
  const newtabEx2 = path.join(ext,"ahejmjlfdnkngmajdfanadcgmdmgahda/2.2.0_0");
  const newtabEx3 = path.join(ext,"bkcbnbbmeifnhooabnknahjpbfblbjfb/1.0.15_0");
  const newtabEx4 = path.join(ext,"pgigkojhmdneillbbpcajchbggkphhjp/2.5_0");

  console.log(newtabEx1)

  // const pathToExtension = path.resolve(ext);  --disable-extensions-except=  ,`--load-extension=${newtabEx1}`.`--custom-launcher-page=${newtabEx1}`,
  const browser = await puppeteer.launch({
    executablePath:exepath,
    ignoreDefaultArgs:true,
    ignoreHTTPSErrors:true,
    headless:false,
    slowMo:20,
    userDataDir: userData,
    args : ['--no-sandbox', '--window-size=1366,850',`--disable-extensions-except=${newtabEx},${newtabEx2},${newtabEx3},${newtabEx4}`]  //主要就是这一句允许例外
  });

  const page = await browser.newPage();
  page.setViewport({'width': 1366, 'height': 768});
  await page.waitFor(2000);
  await page.goto('chrome-extension://milhmifoljmifighlanebdlekdjhbibd/newtab/Home.html');//这一句打开扩展插件
  await page.click("#server-setting");
  await page.type('#domain',"124.193.66.66:30080");
  await page.click('#img_arrow');
  await page.click('#domain_protocal_change>li:nth-child(2)');
  await page.click('#next-btn');  

  await page.waitFor(5000);

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

插件的信息如何查看

方法一、打开sources默认打开当前的路径

puppeteer(六)启动参数——浏览器扩展应用的添加及应用

方法二、console>命令行输入location

puppeteer(六)启动参数——浏览器扩展应用的添加及应用

上一篇:安装mayavi和VTK库的血泪史


下一篇:【微信小程序开发】全局配置