从文件Selenium node.js添加解压缩的扩展名

时间:2018-07-05 00:51:25

标签: node.js selenium selenium-webdriver selenium-chromedriver

我想知道如何使用以下方法将自己在电脑上具有的未压缩扩展名添加到chrome webdriver中: https://seleniumhq.github.io/selenium/docs/api/javascript/module/selenium-webdriver/chrome_exports_Options.html#addExtensions 我真的不明白我应该怎么做。 在此先感谢对我有帮助的所有人=) P.S:我正在使用这些节点程序包:selenium-webdriver,fs

2 个答案:

答案 0 :(得分:1)

import {Builder, Capabilities} from 'selenium-webdriver';
import {Options, ServiceBuilder, setDefaultService} from 'selenium-webdriver/chrome';
import * as chromedriver from 'chromedriver';
import * as path from 'path';
import * as fs from 'fs';

const encodeExt = file => {
  const stream = fs.readFileSync(path.resolve(file));
  return Buffer.from(stream).toString('base64');
};

const service = new ServiceBuilder(chromedriver.path).build();
setDefaultService(service);

const driver = new Builder()
  .withCapabilities(Capabilities.chrome())
  .setChromeOptions(new Options()
    .addExtensions(encodeExt('./3.5.2_0.crx')))        <-----+
  .build();                                                  |
                                                             |
                                                             |
                  this line here! ----------------------------

这是下载.crx文件的方法:https://www.maketecheasier.com/download-save-chrome-extension/

答案 1 :(得分:0)

您可以这样做:

let chrome = require("selenium-webdriver/chrome");
let options = new chrome.Options();
options.addExtensions("/path/to/extension.crx")
let driver = new webdriver.Builder()
    .forBrowser('chrome')
    .setChromeOptions(options)
    .build();