使用Selenium和Ruby自动化Chrome扩展

时间:2016-03-31 19:59:23

标签: ruby google-chrome selenium

我目前正在开发一个自动化项目,我需要使用Ruby / Selenium来发现在对Web应用程序进行身份验证后返回给用户的特定http标头。我能够很好地自动化Web应用程序;但是,当我尝试使用Chrome扩展程序时,浏览器会返回以下错误:

chrome-extension:// [扩展程序地址]的网页可能暂时关闭,或者可能已永久移动到新的网址。

在查看此内容后,似乎Selenium网络驱动程序使用的Chrome配置文件与常规Chrome配置文件不同。因此,我想知道是否有人知道是否有办法告诉Selenium使用我的常规Chrome配置文件加载扩展程序或构建新配置文件并在运行时安装扩展程序。

到目前为止,我发现的大多数答案都集中在Python和Java上。如果我能提供更多信息,请告诉我。

1 个答案:

答案 0 :(得分:1)

要在Windows上使用默认配置文件启动Chrome:

require 'selenium-webdriver'

switches = ['user-data-dir='+ENV['LOCALAPPDATA']+'\\Google\\Chrome\\User Data']
driver = Selenium::WebDriver.for :chrome, :switches => switches

driver.navigate.to "https://www.google.co.uk"

或者为创建的个人资料添加扩展名:

require 'selenium-webdriver'

driver = Selenium::WebDriver.for :chrome, 
  :desired_capabilities => Selenium::WebDriver::Remote::Capabilities.chrome({
    'chromeOptions' => {
      'extensions' => [
        Base64.strict_encode64(File.open('C:\\App\\extension.crx', 'rb').read)
      ]
    }
  })

driver.navigate.to "https://www.google.co.uk"
相关问题