Class: Ritsudo::Driver

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/ritsudo/driver.rb

Instance Method Summary collapse

Constructor Details

#initialize(logger_level: :warn, timeout: 60, wait_time: 5, logger_output: "./ritsudo.selenium.log", user_agent: nil, cookies: nil, args: ['--headless', '--window-size=1920,1080', '--ignore-certificate-errors', '--disable-popup-blocking', '--disable-translate', '--blink-settings=imagesEnabled=false']) ⇒ Driver

Returns a new instance of Driver.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ritsudo/driver.rb', line 14

def initialize(logger_level: :warn, timeout: 60, wait_time: 5,
               logger_output: "./ritsudo.selenium.log",
               user_agent: nil,
               cookies: nil,
               args: ['--headless',
                      '--window-size=1920,1080',
                      '--ignore-certificate-errors',
                      '--disable-popup-blocking',
                      '--disable-translate',
                      '--blink-settings=imagesEnabled=false'])

  if user_agent
    args << "--user-agent=#{user_agent}"
  end

  if cookies
    @cookie_templates = WEBrick::Cookie.parse(cookies).map do |cookie|
      { name: cookie.name, value: cookie.value, path: '/' }
    end
  end

  Selenium::WebDriver.logger.output = logger_output
  Selenium::WebDriver.logger.level = logger_level
  client = Selenium::WebDriver::Remote::Http::Default.new.tap { |c| c.read_timeout = timeout }
  driver_options = { options: options(args), desired_capabilities: caps, http_client: client }
  @driver = Selenium::WebDriver.for(:chrome, driver_options).tap do |d|
    d.manage.timeouts.implicit_wait = timeout
  end
  #@wait = Selenium::WebDriver::Wait.new(timeout: wait_time)
end

Instance Method Details

#driverObject



10
11
12
# File 'lib/ritsudo/driver.rb', line 10

def driver
  @driver
end

#get(url) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ritsudo/driver.rb', line 45

def get(url)
  if @cookie_templates
    u = URI.parse(url)
    base_url = u.to_s.sub(u.request_uri, '/')
    @driver.get(base_url)
    @driver.execute_script("return window.stop");
    sleep(1)
    @driver.manage.logs.get('performance')
    @cookie_templates&.each do |cookie|
      manage.add_cookie(cookie.merge(domain: URI.parse(url).host))
    end
  end
  @driver.get(url)
end