如何在Ruby Selenium中使用Chrome选项?

时间:2017-07-24 08:23:22

标签: ruby selenium selenium-webdriver

以下代码段来自官方网页Ruby Bindings;但它无法正常工作

HashMap<Integer, CellStyle> styleMap = new HashMap<Integer, CellStyle>();
public void copyCell(Cell oldCell, Cell newCell){
       int styleHashCode = oldCell.getCellStyle().hashCode();
           CellStyle newCellStyle = styleMap.get(styleHashCode);
           if(newCellStyle == null){
               newCellStyle = newCell.getSheet().getWorkbook().createCellStyle();
               newCellStyle.cloneStyleFrom(oldCell.getCellStyle());
               styleMap.put(styleHashCode, newCellStyle);
           }
           newCell.setCellStyle(newCellStyle);
}

错误:

enter image description here enter image description here

2 个答案:

答案 0 :(得分:5)

适用于Selenium 4&amp; Chrome <75个用户

  options = {
      args: ['disable-infobars', 'disable-gpu', 'privileged', 'ignore-certificate-errors', 'no-default-browser-check'],
      w3c: true,
      mobileEmulation: {},
      prefs: {
          :protocol_handler => {
              :excluded_schemes => {
                  tel: false,
              }
          }
      },
      extensions: [ Base64.strict_encode64(File.open("../your_extension.crx", 'rb').read) ]
  }

  caps = Selenium::WebDriver::Chrome::Options.new(options: options)
  @driver = Selenium::WebDriver.for(:chrome, options: caps)

适用于Selenium 3用户

使用开关定义chrome选项

  caps = Selenium::WebDriver::Remote::Capabilities.chrome("desiredCapabilities" => {"takesScreenshot" => true}, "chromeOptions" => {"binary" => "/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome"})
  @driver = Selenium::WebDriver.for :chrome, desired_capabilities: caps, switches: %w[--incognito --screen-size=1200x800]

或者

driver = Selenium::WebDriver.for :chrome, switches: %w[--incognito]

<强> RemoteWebDriver

caps = Selenium::WebDriver::Remote::Capabilities.chrome("chromeOptions" => {"args" => [ "disable-infobars" ]})
driver = Selenium::WebDriver.for :remote, url: 'http://localhost:4444/wd/hub', desired_capabilities: caps
  

Chrome切换列表

     

https://peter.sh/experiments/chromium-command-line-switches/

答案 1 :(得分:2)

这对我有用:

args = ['ignore-certificate-errors', 'disable-popup-blocking', 'disable-translate']

options = Selenium::WebDriver::Chrome::Options.new(args: args)
driver = Selenium::WebDriver.for :chrome, options: options