Watir Webdriver加载Chrome扩展程序 [英] Watir Webdriver Load Chrome Extension
问题描述
我试图用Watir加载一个Chrome扩展,并且遇到了问题。
我发现这个相关的问题:能够用扩展程序加载watir-webdriver 启动Chrome。然而,我仍然有同样的问题后。
require'rubygems'
require'watir-webdriver '
require'ruby-debug'
require'nokogiri'
browser = Watir :: Browser.new:chrome,:switches => %w [ - load-extension =〜/ .config / google-chrome / Default / Extensions / 0.1.12_0]
$ b $ sleep(10)
browser.close
我也尝试将扩展名从/ Extensions复制到/ Desktop并从那里加载,无济于事。
我得到的错误是无法从...扩展... Manifest文件丢失或不可读。
Manifest文件确实存在,并且似乎是JSON格式的正确文件。
尝试加载不同的扩展名也会失败。
ol>
将您的扩展程序打包到* .crx文件中。您可以按照此指南,或者只是谷歌如何打包扩展名。
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)
结束
rescue Exception => e
raise错误:无法将File.read或Base64.encode64作为Chrome扩展程序:#{e.message}
end
#将扩展附加到您的功能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)
不要忘记
require'base64' / code>。
该示例适用于远程网络驱动程序实例,但我认为它也适用于本地使用网络驱动程序。只需调整传递给
Watir :: Browser.new
的参数。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.
解决方案If you pack the extension and then base64 it, you can load it into the Chrome browser right from your ruby code.
Pack your extension into a *.crx file. You can follow this guide, or just google how to pack a chrome extension.
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
.这篇关于Watir Webdriver加载Chrome扩展程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!