Class: Kimurai::BrowserBuilder::SeleniumChromeBuilder
- Inherits:
-
Object
- Object
- Kimurai::BrowserBuilder::SeleniumChromeBuilder
- Defined in:
- lib/kimurai/browser_builder/selenium_chrome_builder.rb
Class Attribute Summary collapse
-
.virtual_display ⇒ Object
Returns the value of attribute virtual_display.
Instance Attribute Summary collapse
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#spider ⇒ Object
readonly
Returns the value of attribute spider.
Instance Method Summary collapse
- #build ⇒ Object
-
#initialize(config, spider:) ⇒ SeleniumChromeBuilder
constructor
A new instance of SeleniumChromeBuilder.
Constructor Details
#initialize(config, spider:) ⇒ SeleniumChromeBuilder
Returns a new instance of SeleniumChromeBuilder.
15 16 17 18 19 |
# File 'lib/kimurai/browser_builder/selenium_chrome_builder.rb', line 15 def initialize(config, spider:) @config = config @spider = spider @logger = spider.logger end |
Class Attribute Details
.virtual_display ⇒ Object
Returns the value of attribute virtual_display.
10 11 12 |
# File 'lib/kimurai/browser_builder/selenium_chrome_builder.rb', line 10 def virtual_display @virtual_display end |
Instance Attribute Details
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
13 14 15 |
# File 'lib/kimurai/browser_builder/selenium_chrome_builder.rb', line 13 def logger @logger end |
#spider ⇒ Object (readonly)
Returns the value of attribute spider.
13 14 15 |
# File 'lib/kimurai/browser_builder/selenium_chrome_builder.rb', line 13 def spider @spider end |
Instance Method Details
#build ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/kimurai/browser_builder/selenium_chrome_builder.rb', line 21 def build # Register driver Capybara.register_driver :selenium_chrome do |app| # Create driver options opts = { args: %w[--disable-gpu --no-sandbox --disable-translate] } # Provide custom chrome browser path: if chrome_path = Kimurai.configuration.selenium_chrome_path opts.merge!(binary: chrome_path) end # See all options here: https://seleniumhq.github.io/selenium/docs/api/rb/Selenium/WebDriver/Chrome/Options.html = Selenium::WebDriver::Chrome::Options.new(opts) # Window size if size = @config[:window_size].presence .args << "--window-size=#{size.join(',')}" logger.debug "BrowserBuilder (selenium_chrome): enabled window_size" end # Proxy if proxy = @config[:proxy].presence proxy_string = (proxy.class == Proc ? proxy.call : proxy).strip ip, port, type, user, password = proxy_string.split(":") if %w(http socks5).include?(type) if user.nil? && password.nil? .args << "--proxy-server=#{type}://#{ip}:#{port}" logger.debug "BrowserBuilder (selenium_chrome): enabled #{type} proxy, ip: #{ip}, port: #{port}" else logger.error "BrowserBuilder (selenium_chrome): proxy with authentication doesn't supported by selenium, skipped" end else logger.error "BrowserBuilder (selenium_chrome): wrong type of proxy: #{type}, skipped" end end if proxy_bypass_list = @config[:proxy_bypass_list].presence if proxy .args << "--proxy-bypass-list=#{proxy_bypass_list.join(';')}" logger.debug "BrowserBuilder (selenium_chrome): enabled proxy_bypass_list" else logger.error "BrowserBuilder (selenium_chrome): provide `proxy` to set proxy_bypass_list, skipped" end end # SSL if @config[:ignore_ssl_errors].present? .args << "--ignore-certificate-errors" .args << "--allow-insecure-localhost" logger.debug "BrowserBuilder (selenium_chrome): enabled ignore_ssl_errors" end # Disable images if @config[:disable_images].present? .prefs["profile.managed_default_content_settings.images"] = 2 logger.debug "BrowserBuilder (selenium_chrome): enabled disable_images" end # Headers if @config[:headers].present? logger.warn "BrowserBuilder: (selenium_chrome): custom headers doesn't supported by selenium, skipped" end if user_agent = @config[:user_agent].presence user_agent_string = (user_agent.class == Proc ? user_agent.call : user_agent).strip .args << "--user-agent='#{user_agent_string}'" logger.debug "BrowserBuilder (selenium_chrome): enabled custom user_agent" end # Headless mode if ENV["HEADLESS"] != "false" if @config[:headless_mode] == :virtual_display if Gem::Platform.local.os == "linux" unless self.class.virtual_display require 'headless' self.class.virtual_display = Headless.new(reuse: true, destroy_at_exit: false) self.class.virtual_display.start end logger.debug "BrowserBuilder (selenium_chrome): enabled virtual_display headless_mode" else logger.error "BrowserBuilder (selenium_chrome): virtual_display headless_mode works only " \ "on Linux platform. Browser will run in normal mode. Set `native` mode instead." end else .args << "--headless" logger.debug "BrowserBuilder (selenium_chrome): enabled native headless_mode" end end chromedriver_path = Kimurai.configuration.chromedriver_path || "/usr/local/bin/chromedriver" Capybara::Selenium::Driver.new(app, browser: :chrome, options: , driver_path: chromedriver_path) end # Create browser instance (Capybara session) @browser = Capybara::Session.new(:selenium_chrome) @browser.spider = spider logger.debug "BrowserBuilder (selenium_chrome): created browser instance" if @config[:extensions].present? logger.error "BrowserBuilder (selenium_chrome): `extensions` option not supported by Selenium, skipped" end # Cookies if = @config[:cookies].presence @browser.config. = logger.debug "BrowserBuilder (selenium_chrome): enabled custom cookies" end # Browser instance options # skip_request_errors if skip_errors = @config[:skip_request_errors].presence @browser.config.skip_request_errors = skip_errors logger.debug "BrowserBuilder (selenium_chrome): enabled skip_request_errors" end # retry_request_errors if retry_errors = @config[:retry_request_errors].presence @browser.config.retry_request_errors = retry_errors logger.debug "BrowserBuilder (selenium_chrome): enabled retry_request_errors" end # restart_if if requests_limit = @config.dig(:restart_if, :requests_limit).presence @browser.config.restart_if[:requests_limit] = requests_limit logger.debug "BrowserBuilder (selenium_chrome): enabled restart_if.requests_limit >= #{requests_limit}" end if memory_limit = @config.dig(:restart_if, :memory_limit).presence @browser.config.restart_if[:memory_limit] = memory_limit logger.debug "BrowserBuilder (selenium_chrome): enabled restart_if.memory_limit >= #{memory_limit}" end # before_request clear_cookies if @config.dig(:before_request, :clear_cookies) @browser.config.before_request[:clear_cookies] = true logger.debug "BrowserBuilder (selenium_chrome): enabled before_request.clear_cookies" end # before_request clear_and_set_cookies if @config.dig(:before_request, :clear_and_set_cookies) if = @config[:cookies].presence @browser.config. = @browser.config.before_request[:clear_and_set_cookies] = true logger.debug "BrowserBuilder (selenium_chrome): enabled before_request.clear_and_set_cookies" else logger.error "BrowserBuilder (selenium_chrome): cookies should be present to enable before_request.clear_and_set_cookies, skipped" end end # before_request change_user_agent if @config.dig(:before_request, :change_user_agent) logger.error "BrowserBuilder (selenium_chrome): before_request.change_user_agent option not supported by Selenium, skipped" end # before_request change_proxy if @config.dig(:before_request, :change_proxy) logger.error "BrowserBuilder (selenium_chrome): before_request.change_proxy option not supported by Selenium, skipped" end # before_request delay if delay = @config.dig(:before_request, :delay).presence @browser.config.before_request[:delay] = delay logger.debug "BrowserBuilder (selenium_chrome): enabled before_request.delay" end # encoding if encoding = @config[:encoding] @browser.config.encoding = encoding logger.debug "BrowserBuilder (selenium_chrome): enabled encoding: #{encoding}" end # return Capybara session instance @browser end |