Class: Puppeteer::Frame

Inherits:
Object
  • Object
show all
Defined in:
lib/puppeteer/frame.rb,
sig/_supplementary.rbs,
sig/puppeteer/frame.rbs

Overview

rbs_inline: enabled

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:



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/puppeteer/frame.rb', line 11

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

  @loader_id = ''
  @url = 'about:blank'
  @lifecycle_events = Set.new
  @child_frames = Set.new
  @extension_worlds = {}
  if parent_frame
    parent_frame._child_frames << self
  end

  update_client(client)
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.

Returns:

  • (Object)


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

def client
  @client
end

#extension_worldsObject

Returns the value of attribute extension_worlds.

Returns:

  • (Object)


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

def extension_worlds
  @extension_worlds
end

#frame_managerObject

Returns the value of attribute frame_manager.

Returns:

  • (Object)


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

def frame_manager
  @frame_manager
end

#idObject

Returns the value of attribute id.

Returns:

  • (Object)


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

def id
  @id
end

#lifecycle_eventsObject

Returns the value of attribute lifecycle_events.

Returns:

  • (Object)


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

def lifecycle_events
  @lifecycle_events
end

#loader_idObject

Returns the value of attribute loader_id.

Returns:

  • (Object)


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

def loader_id
  @loader_id
end

#main_worldObject

Returns the value of attribute main_world.

Returns:

  • (Object)


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

def main_world
  @main_world
end

#puppeteer_worldObject

Returns the value of attribute puppeteer_world.

Returns:

  • (Object)


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

def puppeteer_world
  @puppeteer_world
end

Instance Method Details

#==(other) ⇒ Boolean

Parameters:

  • other (Object)

Returns:

  • (Boolean)


85
86
87
88
89
90
91
92
# File 'lib/puppeteer/frame.rb', line 85

def ==(other)
  other = other.__getobj__ if other.is_a?(Puppeteer::ReactorRunner::Proxy)
  return true if equal?(other)
  return false unless other.is_a?(Puppeteer::Frame)
  return false unless @id && other.id

  @id == other.id
end

#_child_framesObject

Returns:

  • (Object)


257
258
259
# File 'lib/puppeteer/frame.rb', line 257

protected def _child_frames
  @child_frames
end

#_clientPuppeteer::CDPSession



40
41
42
# File 'lib/puppeteer/frame.rb', line 40

def _client
  @client
end

#accessibilityPuppeteer::Accessibility



56
57
58
# File 'lib/puppeteer/frame.rb', line 56

def accessibility
  @accessibility ||= Puppeteer::Accessibility.new(self)
end

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

Parameters:

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

Returns:



277
278
279
# File 'lib/puppeteer/frame.rb', line 277

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) ⇒ Puppeteer::ElementHandle

Parameters:

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

Returns:



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

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

#child_framesArray[Puppeteer::Frame]

Returns:



262
263
264
# File 'lib/puppeteer/frame.rb', line 262

def child_frames
  @child_frames.to_a
end

#click(selector, delay: nil, button: nil, count: nil) ⇒ void

This method returns an undefined value.

Parameters:

  • selector (String)
  • delay: (Numeric, nil) (defaults to: nil)
  • button: (String, nil) (defaults to: nil)
  • count: (Integer, nil) (defaults to: nil)


294
295
296
# File 'lib/puppeteer/frame.rb', line 294

def click(selector, delay: nil, button: nil, count: nil)
  @puppeteer_world.click(selector, delay: delay, button: button, count: count)
end

#contentString

Returns:

  • (String)


216
217
218
# File 'lib/puppeteer/frame.rb', line 216

def content
  @puppeteer_world.content
end

#default_timeoutNumeric

Returns:

  • (Numeric)


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

def default_timeout
  @frame_manager.timeout_settings.timeout
end

#detachvoid

This method returns an undefined value.



434
435
436
437
438
439
440
441
442
443
# File 'lib/puppeteer/frame.rb', line 434

def detach
  @detached = true
  @main_world.detach
  @puppeteer_world.detach
  @extension_worlds.each_value(&:detach)
  if @parent_frame
    @parent_frame._child_frames.delete(self)
  end
  @parent_frame = nil
end

#detached?Boolean

Returns:

  • (Boolean)


267
268
269
# File 'lib/puppeteer/frame.rb', line 267

def detached?
  @detached
end

#eval_on_selector(selector, page_function, *args) ⇒ Object Also known as: Seval

$eval() in JavaScript.

Parameters:

  • selector (String)
  • page_function (String)
  • args (Object)

Returns:

  • (Object)


185
186
187
# File 'lib/puppeteer/frame.rb', line 185

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) ⇒ Object Also known as: SSeval

$$eval() in JavaScript.

Parameters:

  • selector (String)
  • page_function (String)
  • args (Object)

Returns:

  • (Object)


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

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:

  • page_function (String)
  • args (Object)

Returns:

  • (Object)


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

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

#evaluate_handle(page_function, *args) ⇒ Puppeteer::JSHandle

Parameters:

  • page_function (String)
  • args (Object)

Returns:



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

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

#execution_contextPuppeteer::ExecutionContext



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

def execution_context
  @main_world.execution_context
end

#extension_realmsArray[untyped]

Returns:

  • (Array[untyped])


239
240
241
# File 'lib/puppeteer/frame.rb', line 239

def extension_realms
  @extension_worlds.values
end

#focus(selector) ⇒ void

This method returns an undefined value.

Parameters:

  • selector (String)


302
303
304
# File 'lib/puppeteer/frame.rb', line 302

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

#frame_elementPuppeteer::ElementHandle?

Returns:



249
250
251
252
253
254
255
# File 'lib/puppeteer/frame.rb', line 249

def frame_element
  parent = parent_frame
  return nil unless parent

  response = parent.client.send_message('DOM.getFrameOwner', frameId: @id)
  parent.main_world.adopt_backend_node(response['backendNodeId'])
end

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

Parameters:

  • url (String)
  • referer: (String, nil) (defaults to: nil)
  • referrer_policy: (String, nil) (defaults to: nil)
  • timeout: (Numeric, nil) (defaults to: nil)
  • wait_until: (String, nil) (defaults to: nil)

Returns:



105
106
107
108
109
110
111
112
113
114
# File 'lib/puppeteer/frame.rb', line 105

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

#handle_lifecycle_event(loader_id, name) ⇒ void

This method returns an undefined value.

Parameters:

  • loader_id (String)
  • name (String)


414
415
416
417
418
419
420
# File 'lib/puppeteer/frame.rb', line 414

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

#handle_loading_startedvoid

This method returns an undefined value.



423
424
425
# File 'lib/puppeteer/frame.rb', line 423

def handle_loading_started
  @has_started_loading = true
end

#handle_loading_stoppedvoid

This method returns an undefined value.



428
429
430
431
# File 'lib/puppeteer/frame.rb', line 428

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

#has_started_loading?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/puppeteer/frame.rb', line 95

def has_started_loading?
  @has_started_loading
end

#hover(selector) ⇒ void

This method returns an undefined value.

Parameters:

  • selector (String)


310
311
312
# File 'lib/puppeteer/frame.rb', line 310

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

#inspectString

Returns:

  • (String)


31
32
33
34
35
36
37
# File 'lib/puppeteer/frame.rb', line 31

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

#locator(selector_or_function) ⇒ Puppeteer::Locator

Parameters:

  • selector_or_function (String)

Returns:



67
68
69
70
71
72
73
# File 'lib/puppeteer/frame.rb', line 67

def locator(selector_or_function)
  if Puppeteer::Locator.function_string?(selector_or_function)
    Puppeteer::FunctionLocator.create(self, selector_or_function)
  else
    Puppeteer::NodeLocator.create(self, selector_or_function)
  end
end

#nameString

Returns:

  • (String)


229
230
231
# File 'lib/puppeteer/frame.rb', line 229

def name
  @name || ''
end

This method returns an undefined value.

Parameters:

  • frame_payload (Hash[String, untyped])


400
401
402
403
# File 'lib/puppeteer/frame.rb', line 400

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

This method returns an undefined value.

Parameters:

  • url (String)


407
408
409
# File 'lib/puppeteer/frame.rb', line 407

def navigated_within_document(url)
  @url = url
end

#oop_frame?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/puppeteer/frame.rb', line 76

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

#pagePuppeteer::Page

Returns:



51
52
53
# File 'lib/puppeteer/frame.rb', line 51

def page
  @frame_manager.page
end

#parent_framePuppeteer::Frame?

Returns:



244
245
246
# File 'lib/puppeteer/frame.rb', line 244

def parent_frame
  @parent_frame
end

#query_selector(selector) ⇒ Puppeteer::ElementHandle? Also known as: S

$() in JavaScript.

Parameters:

  • selector (String)

Returns:



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

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

#query_selector_all(selector, isolate: nil) ⇒ Array[Puppeteer::ElementHandle] Also known as: SS

$$() in JavaScript.

Parameters:

  • selector (String)
  • isolate: (Boolean, nil) (defaults to: nil)

Returns:



208
209
210
# File 'lib/puppeteer/frame.rb', line 208

def query_selector_all(selector, isolate: nil)
  @main_world.query_selector_all(selector, isolate: isolate)
end

#select(selector, *values) ⇒ Array[String]

Parameters:

  • selector (String)
  • values (Object)

Returns:

  • (Array[String])


317
318
319
# File 'lib/puppeteer/frame.rb', line 317

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

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

This method returns an undefined value.

Parameters:

  • html (String)
  • timeout: (Numeric, nil) (defaults to: nil)
  • wait_until: (String, Array[String], nil) (defaults to: nil)


224
225
226
# File 'lib/puppeteer/frame.rb', line 224

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

#Sx(expression) ⇒ Array[Puppeteer::ElementHandle]

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

Parameters:

  • expression (String)

Returns:



167
168
169
170
171
172
173
174
175
176
# File 'lib/puppeteer/frame.rb', line 167

def Sx(expression)
  param_xpath =
    if expression.start_with?('//')
      ".#{expression}"
    else
      expression
    end

  query_selector_all("xpath/#{param_xpath}")
end

#tap(selector) ⇒ void

This method returns an undefined value.

Parameters:

  • selector (String)


325
326
327
# File 'lib/puppeteer/frame.rb', line 325

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

#titleString

Returns:

  • (String)


394
395
396
# File 'lib/puppeteer/frame.rb', line 394

def title
  @puppeteer_world.title
end

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

This method returns an undefined value.

Parameters:

  • selector (String)
  • text (String)
  • delay: (Numeric, nil) (defaults to: nil)


335
336
337
# File 'lib/puppeteer/frame.rb', line 335

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

#urlString?

Returns:

  • (String, nil)


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

def url
  @url
end

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

Parameters:

  • page_function (String)
  • args: (Array[untyped]) (defaults to: [])
  • polling: (String, Numeric, nil) (defaults to: nil)
  • timeout: (Numeric, nil) (defaults to: nil)

Returns:



387
388
389
# File 'lib/puppeteer/frame.rb', line 387

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, ignore_same_document_navigation: false) ⇒ Puppeteer::HTTPResponse?

Parameters:

  • timeout: (Numeric, nil) (defaults to: nil)
  • wait_until: (String, nil) (defaults to: nil)
  • ignore_same_document_navigation: (Boolean) (defaults to: false)

Returns:



120
121
122
123
124
125
126
127
# File 'lib/puppeteer/frame.rb', line 120

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

#wait_for_selector(selector, visible: nil, hidden: nil, timeout: nil) ⇒ Puppeteer::ElementHandle?

Parameters:

  • selector (String)
  • visible: (Boolean, nil) (defaults to: nil)
  • hidden: (Boolean, nil) (defaults to: nil)
  • timeout: (Numeric, nil) (defaults to: nil)

Returns:



346
347
348
349
350
351
352
353
354
# File 'lib/puppeteer/frame.rb', line 346

def wait_for_selector(selector, visible: nil, hidden: nil, timeout: nil)
  query_handler_manager = Puppeteer::QueryHandlerManager.instance
  query_handler_manager.detect_query_handler(selector).wait_for(
    self,
    visible: visible,
    hidden: hidden,
    timeout: timeout,
  )
end

#wait_for_timeout(milliseconds) ⇒ void

This method returns an undefined value.

Parameters:

  • milliseconds (Numeric)


360
361
362
# File 'lib/puppeteer/frame.rb', line 360

def wait_for_timeout(milliseconds)
  Puppeteer::AsyncUtils.sleep_seconds(milliseconds / 1000.0)
end

#wait_for_xpath(xpath, visible: nil, hidden: nil, timeout: nil) ⇒ Puppeteer::ElementHandle?

Parameters:

  • xpath (String)
  • visible: (Boolean, nil) (defaults to: nil)
  • hidden: (Boolean, nil) (defaults to: nil)
  • timeout: (Numeric, nil) (defaults to: nil)

Returns:



369
370
371
372
373
374
375
376
377
378
# File 'lib/puppeteer/frame.rb', line 369

def wait_for_xpath(xpath, visible: nil, hidden: nil, timeout: nil)
  param_xpath =
    if xpath.start_with?('//')
      ".#{xpath}"
    else
      xpath
    end

  wait_for_selector("xpath/#{param_xpath}", visible: visible, hidden: hidden, timeout: timeout)
end