Selenium WebDriver加载插件并保存状态的方法(基于Node.js)

WebDriver每次启动时可能都会创建新的或者使用默认的profile,所以在浏览器中添加了插件,并设置好后下次重启就没有了,需要手动设置一下profile的路径才能够保存,方法以加载插件image block X为例

  1. create firefox profile:start firefox profile manager cd ./profile/firefox firefox -p and create a new profile, select folder “./profile/firefox”, named “firefox-esr”(anything else)
  2. enter “about:config” in address bar of Firefox, search ‘xpi’
    change the following properties to false:
xpinstall.signatures.required
xpinstall.whitelist.required
  1. install image block x: Menu -> Add-ons -> Extensions -> Install add-on from file, select the file: extensions/firefox/image-block-x.xpi
  2. start firefox with the new firefox profile: firefox -p and select the profile “firefox-esr”, “start firefox”
  3. go to setup the white list and click “image block x” button to enable image block and quit firefox
    设置好profile后,在启动webdriver时自动加载
const Selenium = require('selenium-webdriver');
const firefox = require('selenium-webdriver/firefox');

let profile = './profile/xxxx.firefox-esr';
options=new firefox.Options();
if( fs.existsSync(profile)){
     options.setProfile(profile);
}
let builder = new Selenium.Builder().forBrowser(Selenium.Browser.FIREFOX);
let driver = await builder.build();
发布了311 篇原创文章 · 获赞 225 · 访问量 84万+

猜你喜欢

转载自blog.csdn.net/Nocky/article/details/104717376