Class: Puppeteer::CustomQueryHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/puppeteer/custom_query_handler.rb

Instance Method Summary collapse

Constructor Details

#initialize(query_one: nil, query_all: nil) ⇒ CustomQueryHandler

Returns a new instance of CustomQueryHandler.

Parameters:

  • query_one (String) (defaults to: nil)

    JS function (element: Element | Document, selector: string) => Element | null;

  • query_all (String) (defaults to: nil)

    JS function (element: Element | Document, selector: string) => Element[] | NodeListOf<Element>;



4
5
6
7
# File 'lib/puppeteer/custom_query_handler.rb', line 4

def initialize(query_one: nil, query_all: nil)
  @query_one = query_one
  @query_all = query_all
end

Instance Method Details

#query_all(element, selector) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/puppeteer/custom_query_handler.rb', line 32

def query_all(element, selector)
  unless @query_all
    raise NotImplementedError.new("#{self.class}##{__method__} is not implemented.")
  end

  handles = element.evaluate_handle(@query_all, selector)
  properties = handles.properties
  handles.dispose
  properties.values.map(&:as_element).compact
end

#query_all_array(element, selector) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/puppeteer/custom_query_handler.rb', line 43

def query_all_array(element, selector)
  unless @query_all
    raise NotImplementedError.new("#{self.class}##{__method__} is not implemented.")
  end

  handles = element.evaluate_handle(@query_all, selector)
  handles.evaluate_handle('(res) => Array.from(res)')
end

#query_one(element, selector) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/puppeteer/custom_query_handler.rb', line 9

def query_one(element, selector)
  unless @query_one
    raise NotImplementedError.new("#{self.class}##{__method__} is not implemented.")
  end

  handle = element.evaluate_handle(@query_one, selector)
  element = handle.as_element

  if element
    return element
  end
  handle.dispose
  nil
end

#wait_for(dom_world, selector, visible: nil, hidden: nil, timeout: nil) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/puppeteer/custom_query_handler.rb', line 24

def wait_for(dom_world, selector, visible: nil, hidden: nil, timeout: nil)
  unless @query_one
    raise NotImplementedError.new("#{self.class}##{__method__} is not implemented.")
  end

  dom_world.send(:wait_for_selector_in_page, @query_one, selector, visible: visible, hidden: hidden, timeout: timeout)
end