使用Selenium和Ruby自动化Chrome扩展



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

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

经过研究,Selenium web驱动程序使用的Chrome配置文件与我的常规Chrome配置文件不同。因此,我想知道是否有人知道是否有办法告诉Selenium使用我的常规Chrome配置文件并加载扩展,或者构建一个新的配置文件并在运行时安装扩展。

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

在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"

最新更新