Watir Webdriver Load Chrome Extension

五迷三道 提交于 2019-12-11 00:56:29

问题


I'm trying to load a chrome extension with Watir, and I'm having issues. I found this related question: Ability to launch chrome with extensions loaded with watir-webdriver. However, I am still having the same issue after following that.

require 'rubygems'
require 'watir-webdriver'
require 'ruby-debug'
require 'nokogiri'

browser = Watir::Browser.new :chrome, :switches => %w[--load-extension=~/.config/google-chrome/Default/Extensions/anepbdekljkmmimmhbniglnnanmmkoja/0.1.12_0]

sleep(10)
browser.close

I also tried copying the extension from /Extensions to /Desktop and loading from there to no avail.

The error I get is Could not load extension from ... Manifest File Missing or Unreadable. The Manifest file does indeed exist and seems to be a correct file in JSON format.

Trying to load different extensions fails as well.


回答1:


Download the chrome extension crx file,

Store the args or any other option need to pass in the watir_opts hash 
 watir_opts[:extensions] = ['path of *.crx file']
 browser = Watir::Browser.new :chrome, options: watir_opts

This worked for me. Note: I didn't encode using 'base64' gem




回答2:


If you pack the extension and then base64 it, you can load it into the Chrome browser right from your ruby code.

  1. Pack your extension into a *.crx file. You can follow this guide, or just google how to pack a chrome extension.

  2. Base64 it then add it to your desired capabilities list. You could use some code similar to this one:

       chrome_extensions = []
       chrome_extension_path = '\home\user\packed_chrome_extension.crx'
    begin
      File.open(chrome_extension_path, "rb") do |file|
        chrome_extensions << Base64.encode64(file.read.chomp)
      end
    rescue Exception => e
      raise "ERROR: Couldn't File.read or Base64.encode64 a Chrome extension: #{e.message}"
    end
    
    # Append the extensions to your capabilities hash
    my_capabilities.merge!({'chrome.extensions' => chrome_extensions})
    
    desired_capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(my_capabilities)
    
    browser = Watir::Browser.new(:remote, :url => 'http://localhost:4444/wd/hub' :desired_capabilities => desired_capabilities)
    

And don't forget to require 'base64' too.

The example is for a remote web-driver instance, but I think it should work when using web-driver locally too. Just adjust the arguments passed to Watir::Browser.new.



来源:https://stackoverflow.com/questions/14106604/watir-webdriver-load-chrome-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!