Class: Puppeteer::Frame

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(frame_manager, parent_frame, frame_id, client) ⇒ Frame

Returns a new instance of Frame.

Parameters:



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

def initialize(frame_manager, parent_frame, frame_id, client)
  @frame_manager = frame_manager
  @parent_frame = parent_frame
  @id = frame_id
  @detached = false

  @loader_id = ''
  @lifecycle_events = Set.new
  @child_frames = Set.new
  if parent_frame
    parent_frame._child_frames << self
  end

  update_client(client)
end

Instance Attribute Details

#frame_managerObject

Returns the value of attribute frame_manager.



47
48
49
# File 'lib/puppeteer/frame.rb', line 47

def frame_manager
  @frame_manager
end

#idObject

Returns the value of attribute id.



47
48
49
# File 'lib/puppeteer/frame.rb', line 47

def id
  @id
end

#lifecycle_eventsObject

Returns the value of attribute lifecycle_events.



47
48
49
# File 'lib/puppeteer/frame.rb', line 47

def lifecycle_events
  @lifecycle_events
end

#loader_idObject

Returns the value of attribute loader_id.



47
48
49
# File 'lib/puppeteer/frame.rb', line 47

def loader_id
  @loader_id
end

#main_worldObject

Returns the value of attribute main_world.



47
48
49
# File 'lib/puppeteer/frame.rb', line 47

def main_world
  @main_world
end

#secondary_worldObject

Returns the value of attribute secondary_world.



47
48
49
# File 'lib/puppeteer/frame.rb', line 47

def secondary_world
  @secondary_world
end

Instance Method Details

#_clientObject



32
33
34
# File 'lib/puppeteer/frame.rb', line 32

def _client
  @client
end

#add_script_tag(url: nil, path: nil, content: nil, type: nil, id: nil) ⇒ Object

Parameters:

  • url (String?) (defaults to: nil)
  • path (String?) (defaults to: nil)
  • content (String?) (defaults to: nil)
  • type (String?) (defaults to: nil)
  • id (String?) (defaults to: nil)


183
184
185
# File 'lib/puppeteer/frame.rb', line 183

def add_script_tag(url: nil, path: nil, content: nil, type: nil, id: nil)
  @main_world.add_script_tag(url: url, path: path, content: content, type: type, id: id)
end

#add_style_tag(url: nil, path: nil, content: nil) ⇒ Object

Parameters:

  • url (String?) (defaults to: nil)
  • path (String?) (defaults to: nil)
  • content (String?) (defaults to: nil)


190
191
192
# File 'lib/puppeteer/frame.rb', line 190

def add_style_tag(url: nil, path: nil, content: nil)
  @main_world.add_style_tag(url: url, path: path, content: content)
end

#child_framesObject



170
171
172
# File 'lib/puppeteer/frame.rb', line 170

def child_frames
  @child_frames.to_a
end

#click(selector, delay: nil, button: nil, click_count: nil) ⇒ Object

Parameters:

  • selector (String)
  • delay (Number) (defaults to: nil)
  • button (String) (defaults to: nil)

    “left”|“right”|“middle”

  • click_count (Number) (defaults to: nil)


198
199
200
# File 'lib/puppeteer/frame.rb', line 198

def click(selector, delay: nil, button: nil, click_count: nil)
  @secondary_world.click(selector, delay: delay, button: button, click_count: click_count)
end

#contentString

Returns:

  • (String)


140
141
142
# File 'lib/puppeteer/frame.rb', line 140

def content
  @secondary_world.content
end

#detachObject



324
325
326
327
328
329
330
331
332
# File 'lib/puppeteer/frame.rb', line 324

def detach
  @detached = true
  @main_world.detach
  @secondary_world.detach
  if @parent_frame
    @parent_frame._child_frames.delete(self)
  end
  @parent_frame = nil
end

#detached?Boolean

Returns:

  • (Boolean)


174
175
176
# File 'lib/puppeteer/frame.rb', line 174

def detached?
  @detached
end

#eval_on_selector(selector, page_function, *args) ⇒ !Promise<(!Object|undefined)> Also known as: Seval

‘$eval()` in JavaScript.

Parameters:

  • selector (string)
  • pageFunction (Function|string)
  • args (!Array<*>)

Returns:

  • (!Promise<(!Object|undefined)>)


110
111
112
# File 'lib/puppeteer/frame.rb', line 110

def eval_on_selector(selector, page_function, *args)
  @main_world.eval_on_selector(selector, page_function, *args)
end

#eval_on_selector_all(selector, page_function, *args) ⇒ !Promise<(!Object|undefined)> Also known as: SSeval

‘$$eval()` in JavaScript.

Parameters:

  • selector (string)
  • pageFunction (Function|string)
  • args (!Array<*>)

Returns:

  • (!Promise<(!Object|undefined)>)


122
123
124
# File 'lib/puppeteer/frame.rb', line 122

def eval_on_selector_all(selector, page_function, *args)
  @main_world.eval_on_selector_all(selector, page_function, *args)
end

#evaluate(page_function, *args) ⇒ Object

Parameters:

  • pageFunction (Function|string)
  • args (!Array<*>)


80
81
82
# File 'lib/puppeteer/frame.rb', line 80

def evaluate(page_function, *args)
  @main_world.evaluate(page_function, *args)
end

#evaluate_handle(page_function, *args) ⇒ !Promise<!Puppeteer.JSHandle>

Parameters:

  • pageFunction (Function|string)

Returns:



72
73
74
# File 'lib/puppeteer/frame.rb', line 72

def evaluate_handle(page_function, *args)
  @main_world.evaluate_handle(page_function, *args)
end

#execution_contextObject



66
67
68
# File 'lib/puppeteer/frame.rb', line 66

def execution_context
  @main_world.execution_context
end

#focus(selector) ⇒ Object

Parameters:

  • selector (string)


205
206
207
# File 'lib/puppeteer/frame.rb', line 205

def focus(selector)
  @secondary_world.focus(selector)
end

#goto(url, referer: nil, timeout: nil, wait_until: nil) ⇒ Puppeteer::HTTPResponse

Parameters:

  • url (String)
  • rederer (String)
  • timeout (number|nil) (defaults to: nil)
  • wait_until (string|nil) (defaults to: nil)

    ‘load’ | ‘domcontentloaded’ | ‘networkidle0’ | ‘networkidle2’

Returns:



54
55
56
# File 'lib/puppeteer/frame.rb', line 54

def goto(url, referer: nil, timeout: nil, wait_until: nil)
  @frame_manager.navigate_frame(self, url, referer: referer, timeout: timeout, wait_until: wait_until)
end

#handle_lifecycle_event(loader_id, name) ⇒ Object



311
312
313
314
315
316
317
# File 'lib/puppeteer/frame.rb', line 311

def handle_lifecycle_event(loader_id, name)
  if name == 'init'
    @loader_id = loader_id
    @lifecycle_events.clear
  end
  @lifecycle_events << name
end

#handle_loading_stoppedObject



319
320
321
322
# File 'lib/puppeteer/frame.rb', line 319

def handle_loading_stopped
  @lifecycle_events << 'DOMContentLoaded'
  @lifecycle_events << 'load'
end

#hover(selector) ⇒ Object

Parameters:

  • selector (string)


212
213
214
# File 'lib/puppeteer/frame.rb', line 212

def hover(selector)
  @secondary_world.hover(selector)
end

#inspectObject



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

def inspect
  values = %i[id parent_frame detached loader_id lifecycle_events child_frames].map do |sym|
    value = instance_variable_get(:"@#{sym}")
    "@#{sym}=#{value}"
  end
  "#<Puppeteer::Frame #{values.join(' ')}>"
end

#nameString

Returns:

  • (String)


152
153
154
# File 'lib/puppeteer/frame.rb', line 152

def name
  @name || ''
end

Parameters:

  • frame_payload (Hash)


297
298
299
300
301
302
303
304
# File 'lib/puppeteer/frame.rb', line 297

def navigated(frame_payload)
  @name = frame_payload['name']
  @url = "#{frame_payload['url']}#{frame_payload['urlFragment']}"

  # Ensure loaderId updated.
  # The order of [Page.lifecycleEvent name="init"] and [Page.frameNavigated] is random... for some reason...
  @loader_id = frame_payload['loaderId']
end

Parameters:

  • url (String)


307
308
309
# File 'lib/puppeteer/frame.rb', line 307

def navigated_within_document(url)
  @url = url
end

#oop_frame?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/puppeteer/frame.rb', line 43

def oop_frame?
  @client != @frame_manager.client
end

#parent_frameFrame?

Returns:



162
163
164
# File 'lib/puppeteer/frame.rb', line 162

def parent_frame
  @parent_frame
end

#query_selector(selector) ⇒ !Promise<?Puppeteer.ElementHandle> Also known as: S

‘$()` in JavaScript.

Parameters:

  • selector (string)

Returns:



89
90
91
# File 'lib/puppeteer/frame.rb', line 89

def query_selector(selector)
  @main_world.query_selector(selector)
end

#query_selector_all(selector) ⇒ !Promise<!Array<!Puppeteer.ElementHandle>> Also known as: SS

‘$$()` in JavaScript.

Parameters:

  • selector (string)

Returns:



132
133
134
# File 'lib/puppeteer/frame.rb', line 132

def query_selector_all(selector)
  @main_world.query_selector_all(selector)
end

#select(selector, *values) ⇒ !Promise<!Array<string>>

Parameters:

  • selector (string)
  • values (!Array<string>)

Returns:

  • (!Promise<!Array<string>>)


219
220
221
# File 'lib/puppeteer/frame.rb', line 219

def select(selector, *values)
  @secondary_world.select(selector, *values)
end

#set_content(html, timeout: nil, wait_until: nil) ⇒ Object

Parameters:

  • html (String)
  • timeout (Integer) (defaults to: nil)
  • wait_until (String|Array<String>) (defaults to: nil)


147
148
149
# File 'lib/puppeteer/frame.rb', line 147

def set_content(html, timeout: nil, wait_until: nil)
  @secondary_world.set_content(html, timeout: timeout, wait_until: wait_until)
end

#Sx(expression) ⇒ !Promise<!Array<!Puppeteer.ElementHandle>>

‘$x()` in JavaScript. $ is not allowed to use as a method name in Ruby.

Parameters:

  • expression (string)

Returns:



99
100
101
# File 'lib/puppeteer/frame.rb', line 99

def Sx(expression)
  @main_world.Sx(expression)
end

#tap(selector) ⇒ Object

Parameters:

  • selector (string)


226
227
228
# File 'lib/puppeteer/frame.rb', line 226

def tap(selector)
  @secondary_world.tap(selector)
end

#titleString

Returns:

  • (String)


292
293
294
# File 'lib/puppeteer/frame.rb', line 292

def title
  @secondary_world.title
end

#type_text(selector, text, delay: nil) ⇒ Object

Parameters:

  • selector (String)
  • text (String)
  • delay (Number) (defaults to: nil)


235
236
237
# File 'lib/puppeteer/frame.rb', line 235

def type_text(selector, text, delay: nil)
  @main_world.type_text(selector, text, delay: delay)
end

#urlString

Returns:

  • (String)


157
158
159
# File 'lib/puppeteer/frame.rb', line 157

def url
  @url
end

#wait_for_function(page_function, args: [], polling: nil, timeout: nil) ⇒ Puppeteer::JSHandle

Parameters:

  • page_function (String)
  • args (Integer|Array) (defaults to: [])
  • polling (String) (defaults to: nil)
  • timeout (Integer) (defaults to: nil)

Returns:



285
286
287
# File 'lib/puppeteer/frame.rb', line 285

def wait_for_function(page_function, args: [], polling: nil, timeout: nil)
  @main_world.wait_for_function(page_function, args: args, polling: polling, timeout: timeout)
end

#wait_for_navigation(timeout: nil, wait_until: nil) ⇒ Object

Parameters:

  • timeout (number|nil) (defaults to: nil)
  • wait_until (string|nil) (defaults to: nil)

    ‘load’ | ‘domcontentloaded’ | ‘networkidle0’ | ‘networkidle2’



60
61
62
# File 'lib/puppeteer/frame.rb', line 60

def wait_for_navigation(timeout: nil, wait_until: nil)
  @frame_manager.wait_for_frame_navigation(self, timeout: timeout, wait_until: wait_until)
end

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

Parameters:

  • selector (String)
  • visible (Boolean) (defaults to: nil)

    Wait for element visible (not ‘display: none’ nor ‘visibility: hidden’) on true. default to false.

  • hidden (Boolean) (defaults to: nil)

    Wait for element invisible (‘display: none’ nor ‘visibility: hidden’) on true. default to false.

  • timeout (Integer) (defaults to: nil)


245
246
247
248
249
250
251
252
253
254
# File 'lib/puppeteer/frame.rb', line 245

def wait_for_selector(selector, visible: nil, hidden: nil, timeout: nil)
  handle = @secondary_world.wait_for_selector(selector, visible: visible, hidden: hidden, timeout: timeout)
  if !handle
    return nil
  end
  main_execution_context = @main_world.execution_context
  result = main_execution_context.adopt_element_handle(handle)
  handle.dispose
  result
end

#wait_for_timeout(milliseconds) ⇒ Object

Parameters:

  • milliseconds (Integer)

    the number of milliseconds to wait.



259
260
261
# File 'lib/puppeteer/frame.rb', line 259

def wait_for_timeout(milliseconds)
  sleep(milliseconds / 1000.0)
end

#wait_for_xpath(xpath, visible: nil, hidden: nil, timeout: nil) ⇒ Object

Parameters:

  • xpath (String)
  • visible (Boolean) (defaults to: nil)

    Wait for element visible (not ‘display: none’ nor ‘visibility: hidden’) on true. default to false.

  • hidden (Boolean) (defaults to: nil)

    Wait for element invisible (‘display: none’ nor ‘visibility: hidden’) on true. default to false.

  • timeout (Integer) (defaults to: nil)


267
268
269
270
271
272
273
274
275
276
# File 'lib/puppeteer/frame.rb', line 267

def wait_for_xpath(xpath, visible: nil, hidden: nil, timeout: nil)
  handle = @secondary_world.wait_for_xpath(xpath, visible: visible, hidden: hidden, timeout: timeout)
  if !handle
    return nil
  end
  main_execution_context = @main_world.execution_context
  result = main_execution_context.adopt_element_handle(handle)
  handle.dispose
  result
end