Class: Puppeteer::Page

Inherits:
Object
  • Object
show all
Includes:
EventCallbackable, IfPresent
Defined in:
lib/puppeteer/page.rb,
lib/puppeteer/page/pdf_options.rb,
lib/puppeteer/page/screenshot_options.rb,
lib/puppeteer/page/screenshot_task_queue.rb

Defined Under Namespace

Classes: FileChooserTimeoutError, JavaScriptExpression, JavaScriptFunction, PDFOptions, PageError, PrintToPdfIsNotImplementedError, ProtocolStreamReader, ScreenshotOptions, ScreenshotTaskQueue, TargetCrashedError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from IfPresent

#if_present

Methods included from EventCallbackable

#add_event_listener, #emit_event, #observe_first, #on_event, #remove_event_listener

Constructor Details

#initialize(client, target, ignore_https_errors) ⇒ Page

Returns a new instance of Page.

Parameters:



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
# File 'lib/puppeteer/page.rb', line 31

def initialize(client, target, ignore_https_errors)
  @closed = false
  @client = client
  @target = target
  @keyboard = Puppeteer::Keyboard.new(client)
  @mouse = Puppeteer::Mouse.new(client, @keyboard)
  @timeout_settings = Puppeteer::TimeoutSettings.new
  @touchscreen = Puppeteer::TouchScreen.new(client, @keyboard)
  # @accessibility = Accessibility.new(client)
  @frame_manager = Puppeteer::FrameManager.new(client, self, ignore_https_errors, @timeout_settings)
  @emulation_manager = Puppeteer::EmulationManager.new(client)
  # @tracing = Tracing.new(client)
  @page_bindings = {}
  # @coverage = Coverage.new(client)
  @javascript_enabled = true
  @screenshot_task_queue = ScreenshotTaskQueue.new

  @workers = {}
  @client.on_event('Target.attachedToTarget') do |event|
    if event['targetInfo']['type'] != 'worker'
      # If we don't detach from service workers, they will never die.
      await @client.send_message('Target.detachFromTarget', sessionId: event['sessionId'])
      next
    end

    session = Puppeteer::Connection.from_session(@client).session(event['sessionId']) # rubocop:disable Lint/UselessAssignment
    #   const worker = new Worker(session, event.targetInfo.url, this._addConsoleMessage.bind(this), this._handleException.bind(this));
    #   this._workers.set(event.sessionId, worker);
    #   this.emit(PageEmittedEvents::WorkerCreated, worker);
  end
  @client.on_event('Target.detachedFromTarget') do |event|
    session_id = event['sessionId']
    worker = @workers[session_id]
    next unless worker

    emit_event(PageEmittedEvents::WorkerDestroyed, worker)
    @workers.delete(session_id)
  end

  @frame_manager.on_event(FrameManagerEmittedEvents::FrameAttached) do |event|
    emit_event(PageEmittedEvents::FrameAttached, event)
  end
  @frame_manager.on_event(FrameManagerEmittedEvents::FrameDetached) do |event|
    emit_event(PageEmittedEvents::FrameDetached, event)
  end
  @frame_manager.on_event(FrameManagerEmittedEvents::FrameNavigated) do |event|
    emit_event(PageEmittedEvents::FrameNavigated, event)
  end

  network_manager = @frame_manager.network_manager
  network_manager.on_event(NetworkManagerEmittedEvents::Request) do |event|
    emit_event(PageEmittedEvents::Request, event)
  end
  network_manager.on_event(NetworkManagerEmittedEvents::Response) do |event|
    emit_event(PageEmittedEvents::Response, event)
  end
  network_manager.on_event(NetworkManagerEmittedEvents::RequestFailed) do |event|
    emit_event(PageEmittedEvents::RequestFailed, event)
  end
  network_manager.on_event(NetworkManagerEmittedEvents::RequestFinished) do |event|
    emit_event(PageEmittedEvents::RequestFinished, event)
  end
  @file_chooser_interception_is_disabled = false
  @file_chooser_interceptors = Set.new

  @client.on_event('Page.domContentEventFired') do |event|
    emit_event(PageEmittedEvents::DOMContentLoaded)
  end
  @client.on_event('Page.loadEventFired') do |event|
    emit_event(PageEmittedEvents::Load)
  end
  @client.on('Runtime.consoleAPICalled') do |event|
    handle_console_api(event)
  end
  # client.on('Runtime.bindingCalled', event => this._onBindingCalled(event));
  @client.on_event('Page.javascriptDialogOpening') do |event|
    handle_dialog_opening(event)
  end
  @client.on_event('Runtime.exceptionThrown') do |exception|
    handle_exception(exception['exceptionDetails'])
  end
  @client.on_event('Inspector.targetCrashed') do |event|
    handle_target_crashed
  end
  # client.on('Performance.metrics', event => this._emitMetrics(event));
  @client.on_event('Log.entryAdded') do |event|
    handle_log_entry_added(event)
  end
  @client.on_event('Page.fileChooserOpened') do |event|
    handle_file_chooser(event)
  end
  @target.is_closed_promise.then do
    emit_event(PageEmittedEvents::Close)
    @closed = true
  end
end

Instance Attribute Details

#accessibilityObject (readonly)

Returns the value of attribute accessibility.



247
248
249
# File 'lib/puppeteer/page.rb', line 247

def accessibility
  @accessibility
end

#coverageObject (readonly)

Returns the value of attribute coverage.



247
248
249
# File 'lib/puppeteer/page.rb', line 247

def coverage
  @coverage
end

#javascript_enabledObject

Returns the value of attribute javascript_enabled.



204
205
206
# File 'lib/puppeteer/page.rb', line 204

def javascript_enabled
  @javascript_enabled
end

#mouseObject (readonly)

Returns the value of attribute mouse.



1055
1056
1057
# File 'lib/puppeteer/page.rb', line 1055

def mouse
  @mouse
end

#targetObject (readonly)

Returns the value of attribute target.



204
205
206
# File 'lib/puppeteer/page.rb', line 204

def target
  @target
end

#touch_screenObject (readonly)

Returns the value of attribute touch_screen.



247
248
249
# File 'lib/puppeteer/page.rb', line 247

def touch_screen
  @touch_screen
end

#viewportObject

Returns the value of attribute viewport.



835
836
837
# File 'lib/puppeteer/page.rb', line 835

def viewport
  @viewport
end

Class Method Details

.create(client, target, ignore_https_errors, default_viewport) ⇒ !Promise<!Page>

Parameters:

Returns:



19
20
21
22
23
24
25
26
# File 'lib/puppeteer/page.rb', line 19

def self.create(client, target, ignore_https_errors, default_viewport)
  page = Puppeteer::Page.new(client, target, ignore_https_errors)
  page.init
  if default_viewport
    page.viewport = default_viewport
  end
  page
end

Instance Method Details

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

Parameters:

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


383
384
385
# File 'lib/puppeteer/page.rb', line 383

def add_script_tag(url: nil, path: nil, content: nil, type: nil)
  main_frame.add_script_tag(url: url, path: path, content: content, type: type)
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)


390
391
392
# File 'lib/puppeteer/page.rb', line 390

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

#async_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'



637
# File 'lib/puppeteer/page.rb', line 637

define_async_method :async_wait_for_navigation

#async_wait_for_request(url: nil, predicate: nil, timeout: nil) ⇒ Object

Waits until request URL matches or request matches the given predicate.

Waits until request URL matches

wait_for_request(url: 'https://example.com/awesome')

Waits until request matches the given predicate

wait_for_request(predicate: -> (req){ req.url.start_with?('https://example.com/search') })

Parameters:

  • url (String) (defaults to: nil)
  • predicate (Proc(Puppeteer::Request -> Boolean) (defaults to: nil)

    ]



707
# File 'lib/puppeteer/page.rb', line 707

define_async_method :async_wait_for_request

#async_wait_for_response(url: nil, predicate: nil, timeout: nil) ⇒ Object

Parameters:

  • url (String) (defaults to: nil)
  • predicate (Proc(Puppeteer::Request -> Boolean) (defaults to: nil)

    ]



733
# File 'lib/puppeteer/page.rb', line 733

define_async_method :async_wait_for_response

#authenticate(username: nil, password: nil) ⇒ Object

Parameters:

  • username (String?) (defaults to: nil)
  • password (String?) (defaults to: nil)


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

def authenticate(username: nil, password: nil)
  @frame_manager.network_manager.authenticate(username: username, password: password)
end

#browserObject



206
207
208
# File 'lib/puppeteer/page.rb', line 206

def browser
  @target.browser
end

#browser_contextObject



210
211
212
# File 'lib/puppeteer/page.rb', line 210

def browser_context
  @target.browser_context
end

#bypass_csp=(enabled) ⇒ Object

Parameters:

  • enabled (Boolean)


773
774
775
# File 'lib/puppeteer/page.rb', line 773

def bypass_csp=(enabled)
  @client.send_message('Page.setBypassCSP', enabled: enabled)
end

#cache_enabled=(enabled) ⇒ Object

Parameters:

  • enabled (boolean)


883
884
885
# File 'lib/puppeteer/page.rb', line 883

def cache_enabled=(enabled)
  @frame_manager.network_manager.cache_enabled = enabled
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)


1061
1062
1063
# File 'lib/puppeteer/page.rb', line 1061

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

#close(run_before_unload: false) ⇒ Object

Parameters:

  • run_before_unload (Boolean) (defaults to: false)


1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
# File 'lib/puppeteer/page.rb', line 1031

def close(run_before_unload: false)
  unless @client.connection
    raise 'Protocol error: Connection closed. Most likely the page has been closed.'
  end

  if run_before_unload
    @client.send_message('Page.close')
  else
    @client.connection.send_message('Target.closeTarget', targetId: @target.target_id)
    await @target.is_closed_promise

    # @closed sometimes remains false, so wait for @closed = true with 100ms timeout.
    25.times do
      break if @closed
      sleep 0.004
    end
  end
end

#closed?boolean

Returns:

  • (boolean)


1051
1052
1053
# File 'lib/puppeteer/page.rb', line 1051

def closed?
  @closed
end

#contentString

Returns:

  • (String)


595
596
597
# File 'lib/puppeteer/page.rb', line 595

def content
  main_frame.content
end

#content=(html) ⇒ Object

Parameters:

  • html (String)


607
608
609
# File 'lib/puppeteer/page.rb', line 607

def content=(html)
  main_frame.set_content(html)
end

#cookies(*urls) ⇒ Array<Hash>

Returns:

  • (Array<Hash>)


351
352
353
# File 'lib/puppeteer/page.rb', line 351

def cookies(*urls)
  @client.send_message('Network.getCookies', urls: (urls.empty? ? [url] : urls))['cookies']
end

#default_navigation_timeout=(timeout) ⇒ Object

Parameters:

  • timeout (number)


273
274
275
# File 'lib/puppeteer/page.rb', line 273

def default_navigation_timeout=(timeout)
  @timeout_settings.default_navigation_timeout = timeout
end

#default_timeout=(timeout) ⇒ Object

Parameters:

  • timeout (number)


278
279
280
# File 'lib/puppeteer/page.rb', line 278

def default_timeout=(timeout)
  @timeout_settings.default_timeout = timeout
end


355
356
357
358
359
360
361
362
# File 'lib/puppeteer/page.rb', line 355

def delete_cookie(*cookies)
  page_url = url
  starts_with_http = page_url.start_with?("http")
  cookies.each do |cookie|
    item = (starts_with_http ? { url: page_url } : {}).merge(cookie)
    @client.send_message("Network.deleteCookies", item)
  end
end

#emulate(device) ⇒ Object

Parameters:



760
761
762
763
# File 'lib/puppeteer/page.rb', line 760

def emulate(device)
  self.viewport = device.viewport
  self.user_agent = device.user_agent
end

#emulate_idle_state(is_user_active: nil, is_screen_unlocked: nil) ⇒ Object

Parameters:

  • is_user_active (Boolean) (defaults to: nil)
  • is_screen_unlocked (Boolean) (defaults to: nil)


815
816
817
818
819
820
821
822
823
824
825
826
# File 'lib/puppeteer/page.rb', line 815

def emulate_idle_state(is_user_active: nil, is_screen_unlocked: nil)
  overrides = {
    isUserActive: is_user_active,
    isScreenUnlocked: is_screen_unlocked,
  }.compact

  if overrides.empty?
    @client.send_message('Emulation.clearIdleOverride')
  else
    @client.send_message('Emulation.setIdleOverride', overrides)
  end
end

#emulate_media_type(media_type) ⇒ Object

Parameters:

  • media_type (String|Symbol|nil)

    either of (media, print, nil)



778
779
780
781
782
783
784
# File 'lib/puppeteer/page.rb', line 778

def emulate_media_type(media_type)
  media_type_str = media_type.to_s
  unless ['screen', 'print', ''].include?(media_type_str)
    raise ArgumentError.new("Unsupported media type: #{media_type}")
  end
  @client.send_message('Emulation.setEmulatedMedia', media: media_type_str)
end

#emulate_timezone(timezone_id) ⇒ Object

Parameters:

  • timezone_id (String?)


803
804
805
806
807
808
809
810
811
# File 'lib/puppeteer/page.rb', line 803

def emulate_timezone(timezone_id)
  @client.send_message('Emulation.setTimezoneOverride', timezoneId: timezone_id || '')
rescue => err
  if err.message.include?('Invalid timezone')
    raise ArgumentError.new("Invalid timezone ID: #{timezone_id}")
  else
    raise err
  end
end

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

`$eval()` in JavaScript.

Parameters:

  • selector (String)
  • page_function (String)

Returns:

  • (Object)


323
324
325
# File 'lib/puppeteer/page.rb', line 323

def eval_on_selector(selector, page_function, *args)
  main_frame.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)

Returns:

  • (Object)


334
335
336
# File 'lib/puppeteer/page.rb', line 334

def eval_on_selector_all(selector, page_function, *args)
  main_frame.eval_on_selector_all(selector, page_function, *args)
end

#evaluate(page_function, *args) ⇒ !Promise<*>

Parameters:

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

Returns:

  • (!Promise<*>)


840
841
842
# File 'lib/puppeteer/page.rb', line 840

def evaluate(page_function, *args)
  main_frame.evaluate(page_function, *args)
end

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

Parameters:

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

Returns:



305
306
307
308
# File 'lib/puppeteer/page.rb', line 305

def evaluate_handle(page_function, *args)
  context = main_frame.execution_context
  context.evaluate_handle(page_function, *args)
end

#evaluate_on_new_document(page_function, *args) ⇒ Object



871
872
873
874
875
876
877
878
879
880
# File 'lib/puppeteer/page.rb', line 871

def evaluate_on_new_document(page_function, *args)
  source =
    if ['=>', 'async', 'function'].any? { |keyword| page_function.include?(keyword) }
      JavaScriptFunction.new(page_function, args).source
    else
      JavaScriptExpression.new(page_function).source
    end

  @client.send_message('Page.addScriptToEvaluateOnNewDocument', source: source)
end

#extra_http_headers=(headers) ⇒ Object

Parameters:

  • headers (Hash)


433
434
435
# File 'lib/puppeteer/page.rb', line 433

def extra_http_headers=(headers)
  @frame_manager.network_manager.extra_http_headers = headers
end

#focus(selector) ⇒ Object

Parameters:

  • selector (string)


1068
1069
1070
# File 'lib/puppeteer/page.rb', line 1068

def focus(selector)
  main_frame.focus(selector)
end

#framesObject



255
256
257
# File 'lib/puppeteer/page.rb', line 255

def frames
  @frame_manager.frames
end

#geolocation=(geolocation) ⇒ Object

Parameters:



200
201
202
# File 'lib/puppeteer/page.rb', line 200

def geolocation=(geolocation)
  @client.send_message('Emulation.setGeolocationOverride', geolocation.to_h)
end

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

Parameters:

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

    'load' | 'domcontentloaded' | 'networkidle0' | 'networkidle2'



737
738
739
# File 'lib/puppeteer/page.rb', line 737

def go_back(timeout: nil, wait_until: nil)
  go(-1, timeout: timeout, wait_until: wait_until)
end

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

Parameters:

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

    'load' | 'domcontentloaded' | 'networkidle0' | 'networkidle2'



743
744
745
# File 'lib/puppeteer/page.rb', line 743

def go_forward(timeout: nil, wait_until: nil)
  go(+1, timeout: timeout, wait_until: wait_until)
end

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

Parameters:

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

    'load' | 'domcontentloaded' | 'networkidle0' | 'networkidle2'



615
616
617
# File 'lib/puppeteer/page.rb', line 615

def goto(url, referer: nil, timeout: nil, wait_until: nil)
  main_frame.goto(url, referer: referer, timeout: timeout, wait_until: wait_until)
end

#handle_file_chooser(event) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/puppeteer/page.rb', line 155

def handle_file_chooser(event)
  return if @file_chooser_interceptors.empty?

  frame = @frame_manager.frame(event['frameId'])
  context = frame.execution_context
  element = context.adopt_backend_node_id(event['backendNodeId'])
  interceptors = @file_chooser_interceptors.to_a
  @file_chooser_interceptors.clear
  file_chooser = Puppeteer::FileChooser.new(element, event)
  interceptors.each do |promise|
    promise.fulfill(file_chooser)
  end
end

#hover(selector) ⇒ Object

Parameters:

  • selector (string)


1075
1076
1077
# File 'lib/puppeteer/page.rb', line 1075

def hover(selector)
  main_frame.hover(selector)
end

#initObject



128
129
130
131
132
133
134
135
# File 'lib/puppeteer/page.rb', line 128

def init
  await_all(
    @frame_manager.async_init,
    @client.async_send_message('Target.setAutoAttach', autoAttach: true, waitForDebuggerOnStart: false, flatten: true),
    @client.async_send_message('Performance.enable'),
    @client.async_send_message('Log.enable'),
  )
end

#keyboard(&block) ⇒ Object



249
250
251
252
253
# File 'lib/puppeteer/page.rb', line 249

def keyboard(&block)
  @keyboard.instance_eval(&block) unless block.nil?

  @keyboard
end

#main_frameObject



243
244
245
# File 'lib/puppeteer/page.rb', line 243

def main_frame
  @frame_manager.main_frame
end

#offline_mode=(enabled) ⇒ Object



268
269
270
# File 'lib/puppeteer/page.rb', line 268

def offline_mode=(enabled)
  @frame_manager.network_manager.offline_mode = enabled
end

#on(event_name, &block) ⇒ Object

Parameters:

  • event_name (Symbol)


138
139
140
141
142
143
144
# File 'lib/puppeteer/page.rb', line 138

def on(event_name, &block)
  unless PageEmittedEvents.values.include?(event_name.to_s)
    raise ArgumentError.new("Unknown event name: #{event_name}. Known events are #{PageEmittedEvents.values.to_a.join(", ")}")
  end

  super(event_name.to_s, &block)
end

#once(event_name, &block) ⇒ Object

Parameters:

  • event_name (Symbol)


147
148
149
150
151
152
153
# File 'lib/puppeteer/page.rb', line 147

def once(event_name, &block)
  unless PageEmittedEvents.values.include?(event_name.to_s)
    raise ArgumentError.new("Unknown event name: #{event_name}. Known events are #{PageEmittedEvents.values.to_a.join(", ")}")
  end

  super(event_name.to_s, &block)
end

#pdf(options = {}) ⇒ String

Returns:

  • (String)


1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
# File 'lib/puppeteer/page.rb', line 1018

def pdf(options = {})
  pdf_options = PDFOptions.new(options)
  result = @client.send_message('Page.printToPDF', pdf_options.page_print_args)
  ProtocolStreamReader.new(client: @client, handle: result['stream'], path: pdf_options.path).read
rescue => err
  if err.message.include?('PrintToPDF is not implemented')
    raise PrintToPdfIsNotImplementedError.new
  else
    raise
  end
end

#query_objects(prototype_handle) ⇒ !Promise<!Puppeteer.JSHandle>

Parameters:

Returns:



314
315
316
317
# File 'lib/puppeteer/page.rb', line 314

def query_objects(prototype_handle)
  context = main_frame.execution_context
  context.query_objects(prototype_handle)
end

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

`$()` in JavaScript.

Parameters:

  • selector (string)

Returns:



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

def query_selector(selector)
  main_frame.query_selector(selector)
end

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

`$$()` in JavaScript.

Parameters:

  • selector (string)

Returns:



295
296
297
# File 'lib/puppeteer/page.rb', line 295

def query_selector_all(selector)
  main_frame.query_selector_all(selector)
end

#reload(timeout: nil, wait_until: nil) ⇒ Puppeteer::Response

Parameters:

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

    'load' | 'domcontentloaded' | 'networkidle0' | 'networkidle2'

Returns:



622
623
624
625
626
627
# File 'lib/puppeteer/page.rb', line 622

def reload(timeout: nil, wait_until: nil)
  await_all(
    async_wait_for_navigation(timeout: timeout, wait_until: wait_until),
    @client.async_send_message('Page.reload'),
  ).first
end

#request_interception=(value) ⇒ Object

Parameters:

  • value (Bool)


264
265
266
# File 'lib/puppeteer/page.rb', line 264

def request_interception=(value)
  @frame_manager.network_manager.request_interception = value
end

#screenshot(type: nil, path: nil, full_page: nil, clip: nil, quality: nil, omit_background: nil, encoding: nil) ⇒ Object

Parameters:

  • type (String) (defaults to: nil)

    “png”|“jpeg”

  • path (String) (defaults to: nil)
  • full_page (Boolean) (defaults to: nil)
  • clip (Hash) (defaults to: nil)
  • quality (Integer) (defaults to: nil)
  • omit_background (Boolean) (defaults to: nil)
  • encoding (String) (defaults to: nil)


899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
# File 'lib/puppeteer/page.rb', line 899

def screenshot(type: nil, path: nil, full_page: nil, clip: nil, quality: nil, omit_background: nil, encoding: nil)
  options = {
    type: type,
    path: path,
    full_page: full_page,
    clip: clip,
    quality:  quality,
    omit_background: omit_background,
    encoding: encoding,
  }.compact
  screenshot_options = ScreenshotOptions.new(options)

  @screenshot_task_queue.post_task do
    screenshot_task(screenshot_options.type, screenshot_options)
  end
end

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

Parameters:

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

Returns:

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


1082
1083
1084
# File 'lib/puppeteer/page.rb', line 1082

def select(selector, *values)
  main_frame.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)


602
603
604
# File 'lib/puppeteer/page.rb', line 602

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


364
365
366
367
368
369
370
371
372
373
374
375
376
377
# File 'lib/puppeteer/page.rb', line 364

def set_cookie(*cookies)
  page_url = url
  starts_with_http = page_url.start_with?("http")
  items = cookies.map do |cookie|
    (starts_with_http ? { url: page_url } : {}).merge(cookie).tap do |item|
      raise ArgumentError.new("Blank page can not have cookie \"#{item[:name]}\"") if item[:url] == "about:blank"
      raise ArgumetnError.new("Data URL page can not have cookie \"#{item[:name]}\"") if item[:url]&.start_with?("data:")
    end
  end
  delete_cookie(*items)
  unless items.empty?
    @client.send_message("Network.setCookies", cookies: items)
  end
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:



344
345
346
# File 'lib/puppeteer/page.rb', line 344

def Sx(expression)
  main_frame.Sx(expression)
end

#tap(selector: nil, &block) ⇒ Object

Parameters:

  • selector (String) (defaults to: nil)


1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
# File 'lib/puppeteer/page.rb', line 1089

def tap(selector: nil, &block)
  # resolves double meaning of tap.
  if selector.nil? && block
    # Original usage of Object#tap.
    #
    # browser.new_page.tap do |page|
    #   ...
    # end
    super(&block)
  else
    # Puppeteer's Page#tap.
    main_frame.tap(selector)
  end
end

#titleString

Returns:

  • (String)


888
889
890
# File 'lib/puppeteer/page.rb', line 888

def title
  main_frame.title
end

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

Parameters:

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


1109
1110
1111
# File 'lib/puppeteer/page.rb', line 1109

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

#urlString

Returns:

  • (String)


590
591
592
# File 'lib/puppeteer/page.rb', line 590

def url
  main_frame.url
end

#user_agent=(user_agent) ⇒ Object

Parameters:

  • user_agent (String)


438
439
440
# File 'lib/puppeteer/page.rb', line 438

def user_agent=(user_agent)
  @frame_manager.network_manager.user_agent = user_agent
end

#wait_for_file_chooser(timeout: nil) ⇒ Puppeteer::FileChooser

Parameters:

  • timeout (Integer) (defaults to: nil)

Returns:



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/puppeteer/page.rb', line 177

def wait_for_file_chooser(timeout: nil)
  if @file_chooser_interceptors.empty?
    @client.send_message('Page.setInterceptFileChooserDialog', enabled: true)
  end

  option_timeout = timeout || @timeout_settings.timeout
  promise = resolvable_future
  @file_chooser_interceptors << promise

  begin
    Timeout.timeout(option_timeout / 1000.0) do
      promise.value!
    end
  rescue Timeout::Error
    raise FileChooserTimeoutError.new(timeout: option_timeout)
  ensure
    @file_chooser_interceptors.delete(promise)
  end
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:



1145
1146
1147
# File 'lib/puppeteer/page.rb', line 1145

def wait_for_function(page_function, args: [], polling: nil, timeout: nil)
  main_frame.wait_for_function(page_function, args: args, polling: polling, timeout: timeout)
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)


1119
1120
1121
# File 'lib/puppeteer/page.rb', line 1119

def wait_for_selector(selector, visible: nil, hidden: nil, timeout: nil)
  main_frame.wait_for_selector(selector, visible: visible, hidden: hidden, timeout: timeout)
end

#wait_for_timeout(milliseconds) ⇒ Object

Parameters:

  • milliseconds (Integer)

    the number of milliseconds to wait.



1126
1127
1128
# File 'lib/puppeteer/page.rb', line 1126

def wait_for_timeout(milliseconds)
  main_frame.wait_for_timeout(milliseconds)
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)


1134
1135
1136
# File 'lib/puppeteer/page.rb', line 1134

def wait_for_xpath(xpath, visible: nil, hidden: nil, timeout: nil)
  main_frame.wait_for_xpath(xpath, visible: visible, hidden: hidden, timeout: timeout)
end

#workersObject



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

def workers
  @workers.values
end