Class: Puppeteer::Page
- Inherits:
-
Object
show all
- Includes:
- DebugPrint, EventCallbackable, IfPresent
- Defined in:
- lib/puppeteer/page.rb,
lib/puppeteer/page/metrics.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, Metrics, MetricsEvent, PDFOptions, PageError, PrintToPdfIsNotImplementedError, ScreenshotOptions, ScreenshotTaskQueue, TargetCrashedError
Constant Summary
collapse
- VISION_DEFICIENCY_TYPES =
%w[
none
achromatopsia
blurredVision
deuteranopia
protanopia
tritanopia
].freeze
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#add_script_tag(url: nil, path: nil, content: nil, type: nil, id: nil) ⇒ Object
-
#add_style_tag(url: nil, path: nil, content: nil) ⇒ Object
-
#async_wait_for_frame(url: nil, predicate: nil, timeout: nil) ⇒ Object
-
#async_wait_for_navigation(timeout: nil, wait_until: nil) ⇒ Object
-
#async_wait_for_request(url: nil, predicate: nil, timeout: nil) ⇒ Object
Waits until request URL matches or request matches the given predicate.
-
#async_wait_for_response(url: nil, predicate: nil, timeout: nil) ⇒ Object
-
#authenticate(username: nil, password: nil) ⇒ Object
-
#bring_to_front ⇒ Object
Brings page to front (activates tab).
-
#browser ⇒ Object
-
#browser_context ⇒ Object
-
#bypass_csp=(enabled) ⇒ Object
-
#cache_enabled=(enabled) ⇒ Object
-
#click(selector, delay: nil, button: nil, click_count: nil) ⇒ Object
-
#close(run_before_unload: false) ⇒ Object
-
#closed? ⇒ boolean
-
#content ⇒ String
-
#content=(html) ⇒ Object
-
#cookies(*urls) ⇒ Array<Hash>
-
#create_pdf_stream(options = {}) ⇒ Enumerable<String>
-
#default_navigation_timeout=(timeout) ⇒ Object
-
#default_timeout=(timeout) ⇒ Object
-
#delete_cookie(*cookies) ⇒ Object
-
#drag_interception_enabled=(enabled) ⇒ Object
-
#drag_interception_enabled? ⇒ Boolean
(also: #drag_interception_enabled)
-
#emulate(device) ⇒ Object
-
#emulate_cpu_throttling(factor) ⇒ Object
-
#emulate_idle_state(is_user_active: nil, is_screen_unlocked: nil) ⇒ Object
-
#emulate_media_features(features) ⇒ Object
-
#emulate_media_type(media_type) ⇒ Object
-
#emulate_network_conditions(network_condition) ⇒ Object
-
#emulate_timezone(timezone_id) ⇒ Object
-
#emulate_vision_deficiency(vision_deficiency_type) ⇒ Object
-
#eval_on_selector(selector, page_function, *args) ⇒ Object
(also: #Seval)
-
#eval_on_selector_all(selector, page_function, *args) ⇒ Object
(also: #SSeval)
‘$$eval()` in JavaScript.
-
#evaluate(page_function, *args) ⇒ !Promise<*>
-
#evaluate_handle(page_function, *args) ⇒ !Promise<!Puppeteer.JSHandle>
-
#evaluate_on_new_document(page_function, *args) ⇒ Object
-
#expose_function(name, puppeteer_function) ⇒ Object
-
#extra_http_headers=(headers) ⇒ Object
-
#focus(selector) ⇒ Object
-
#frames ⇒ Object
-
#geolocation=(geolocation) ⇒ Object
-
#go_back(timeout: nil, wait_until: nil) ⇒ Object
-
#go_forward(timeout: nil, wait_until: nil) ⇒ Object
-
#goto(url, referer: nil, timeout: nil, wait_until: nil) ⇒ Object
-
#handle_binding_called(event) ⇒ Object
-
#handle_file_chooser(event) ⇒ Object
-
#hover(selector) ⇒ Object
-
#init ⇒ Object
-
#initialize(client, target, ignore_https_errors) ⇒ Page
constructor
-
#keyboard(&block) ⇒ Object
-
#main_frame ⇒ Object
-
#metrics ⇒ Object
-
#offline_mode=(enabled) ⇒ Object
-
#on(event_name, &block) ⇒ Object
-
#once(event_name, &block) ⇒ Object
-
#pdf(options = {}) ⇒ String
-
#query_objects(prototype_handle) ⇒ !Promise<!Puppeteer.JSHandle>
-
#query_selector(selector) ⇒ !Promise<?Puppeteer.ElementHandle>
(also: #S)
-
#query_selector_all(selector) ⇒ !Promise<!Array<!Puppeteer.ElementHandle>>
(also: #SS)
-
#reload(timeout: nil, wait_until: nil) ⇒ Puppeteer::HTTPResponse
-
#request_interception=(value) ⇒ Object
-
#screenshot(type: nil, path: nil, full_page: nil, clip: nil, quality: nil, omit_background: nil, encoding: nil) ⇒ Object
-
#select(selector, *values) ⇒ !Promise<!Array<string>>
-
#set_content(html, timeout: nil, wait_until: nil) ⇒ Object
-
#set_cookie(*cookies) ⇒ Object
-
#set_user_agent(user_agent, user_agent_metadata = nil) ⇒ Object
(also: #user_agent=)
-
#Sx(expression) ⇒ !Promise<!Array<!Puppeteer.ElementHandle>>
-
#tap(selector: nil, &block) ⇒ Object
-
#title ⇒ String
-
#type_text(selector, text, delay: nil) ⇒ Object
-
#url ⇒ String
-
#wait_for_file_chooser(timeout: nil) ⇒ Puppeteer::FileChooser
-
#wait_for_frame(url: nil, predicate: nil, timeout: nil) ⇒ Object
-
#wait_for_function(page_function, args: [], polling: nil, timeout: nil) ⇒ Puppeteer::JSHandle
-
#wait_for_navigation(timeout: nil, wait_until: nil) ⇒ Object
-
#wait_for_request(url: nil, predicate: nil, timeout: nil) ⇒ Object
-
#wait_for_response(url: nil, predicate: nil, timeout: nil) ⇒ Object
-
#wait_for_selector(selector, visible: nil, hidden: nil, timeout: nil) ⇒ Object
-
#wait_for_timeout(milliseconds) ⇒ Object
-
#wait_for_xpath(xpath, visible: nil, hidden: nil, timeout: nil) ⇒ Object
-
#workers ⇒ Object
Methods included from IfPresent
#if_present
#add_event_listener, #emit_event, #observe_first, #on_event, #remove_event_listener
Methods included from DebugPrint
#debug_print, #debug_puts
Constructor Details
#initialize(client, target, ignore_https_errors) ⇒ Page
Returns a new instance of Page.
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
# File 'lib/puppeteer/page.rb', line 33
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)
@frame_manager = Puppeteer::FrameManager.new(client, self, ignore_https_errors, @timeout_settings)
@emulation_manager = Puppeteer::EmulationManager.new(client)
@tracing = Puppeteer::Tracing.new(client)
@page_bindings = {}
@coverage = Puppeteer::Coverage.new(client)
@javascript_enabled = true
@screenshot_task_queue = ScreenshotTaskQueue.new
@workers = {}
@user_drag_interception_enabled = false
@client.add_event_listener('Target.attachedToTarget') do |event|
if event['targetInfo']['type'] != 'worker' && event['targetInfo']['type'] != 'iframe'
@client.async_send_message('Target.detachFromTarget', sessionId: event['sessionId'])
next
end
if event['targetInfo']['type'] == 'worker'
session = Puppeteer::Connection.from_session(@client).session(event['sessionId'])
end
end
@client.add_event_listener('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.add_event_listener('Runtime.consoleAPICalled') do |event|
handle_console_api(event)
end
@client.add_event_listener('Runtime.bindingCalled') do |event|
handle_binding_called(event)
end
@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_event('Performance.metrics') do |event|
emit_event(PageEmittedEvents::Metrics, MetricsEvent.new(event))
end
@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
#accessibility ⇒ Object
Returns the value of attribute accessibility.
274
275
276
|
# File 'lib/puppeteer/page.rb', line 274
def accessibility
@accessibility
end
|
#coverage ⇒ Object
Returns the value of attribute coverage.
274
275
276
|
# File 'lib/puppeteer/page.rb', line 274
def coverage
@coverage
end
|
#javascript_enabled ⇒ Object
Also known as:
javascript_enabled?
Returns the value of attribute javascript_enabled.
230
231
232
|
# File 'lib/puppeteer/page.rb', line 230
def javascript_enabled
@javascript_enabled
end
|
#mouse ⇒ Object
Returns the value of attribute mouse.
1171
1172
1173
|
# File 'lib/puppeteer/page.rb', line 1171
def mouse
@mouse
end
|
#target ⇒ Object
Returns the value of attribute target.
230
231
232
|
# File 'lib/puppeteer/page.rb', line 230
def target
@target
end
|
#touch_screen ⇒ Object
Returns the value of attribute touch_screen.
274
275
276
|
# File 'lib/puppeteer/page.rb', line 274
def touch_screen
@touch_screen
end
|
#tracing ⇒ Object
Returns the value of attribute tracing.
274
275
276
|
# File 'lib/puppeteer/page.rb', line 274
def tracing
@tracing
end
|
#viewport ⇒ Object
Returns the value of attribute viewport.
946
947
948
|
# File 'lib/puppeteer/page.rb', line 946
def viewport
@viewport
end
|
Class Method Details
.create(client, target, ignore_https_errors, default_viewport) ⇒ !Promise<!Page>
21
22
23
24
25
26
27
28
|
# File 'lib/puppeteer/page.rb', line 21
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, id: nil) ⇒ Object
421
422
423
|
# File 'lib/puppeteer/page.rb', line 421
def add_script_tag(url: nil, path: nil, content: nil, type: nil, id: nil)
main_frame.add_script_tag(url: url, path: path, content: content, type: type, id: id)
end
|
#add_style_tag(url: nil, path: nil, content: nil) ⇒ Object
428
429
430
|
# File 'lib/puppeteer/page.rb', line 428
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_frame(url: nil, predicate: nil, timeout: nil) ⇒ Object
815
|
# File 'lib/puppeteer/page.rb', line 815
define_async_method :async_wait_for_frame
|
#async_wait_for_navigation(timeout: nil, wait_until: nil) ⇒ Object
669
|
# File 'lib/puppeteer/page.rb', line 669
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') })
761
|
# File 'lib/puppeteer/page.rb', line 761
define_async_method :async_wait_for_request
|
#async_wait_for_response(url: nil, predicate: nil, timeout: nil) ⇒ Object
787
|
# File 'lib/puppeteer/page.rb', line 787
define_async_method :async_wait_for_response
|
#authenticate(username: nil, password: nil) ⇒ Object
480
481
482
|
# File 'lib/puppeteer/page.rb', line 480
def authenticate(username: nil, password: nil)
@frame_manager.network_manager.authenticate(username: username, password: password)
end
|
#bring_to_front ⇒ Object
Brings page to front (activates tab).
841
842
843
|
# File 'lib/puppeteer/page.rb', line 841
def bring_to_front
@client.send_message('Page.bringToFront')
end
|
#browser ⇒ Object
233
234
235
|
# File 'lib/puppeteer/page.rb', line 233
def browser
@target.browser
end
|
#browser_context ⇒ Object
237
238
239
|
# File 'lib/puppeteer/page.rb', line 237
def browser_context
@target.browser_context
end
|
#bypass_csp=(enabled) ⇒ Object
859
860
861
|
# File 'lib/puppeteer/page.rb', line 859
def bypass_csp=(enabled)
@client.send_message('Page.setBypassCSP', enabled: enabled)
end
|
#cache_enabled=(enabled) ⇒ Object
994
995
996
|
# File 'lib/puppeteer/page.rb', line 994
def cache_enabled=(enabled)
@frame_manager.network_manager.cache_enabled = enabled
end
|
#click(selector, delay: nil, button: nil, click_count: nil) ⇒ Object
1177
1178
1179
|
# File 'lib/puppeteer/page.rb', line 1177
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
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
|
# File 'lib/puppeteer/page.rb', line 1147
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
25.times do
break if @closed
sleep 0.004
end
end
end
|
#closed? ⇒ boolean
1167
1168
1169
|
# File 'lib/puppeteer/page.rb', line 1167
def closed?
@closed
end
|
#content ⇒ String
628
629
630
|
# File 'lib/puppeteer/page.rb', line 628
def content
main_frame.content
end
|
#content=(html) ⇒ Object
640
641
642
|
# File 'lib/puppeteer/page.rb', line 640
def content=(html)
main_frame.set_content(html)
end
|
#cookies(*urls) ⇒ Array<Hash>
388
389
390
|
# File 'lib/puppeteer/page.rb', line 388
def cookies(*urls)
@client.send_message('Network.getCookies', urls: (urls.empty? ? [url] : urls))['cookies']
end
|
#create_pdf_stream(options = {}) ⇒ Enumerable<String>
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
|
# File 'lib/puppeteer/page.rb', line 1096
def create_pdf_stream(options = {})
timeout_helper = Puppeteer::TimeoutHelper.new('Page.printToPDF',
timeout_ms: options[:timeout],
default_timeout_ms: 30000)
pdf_options = PDFOptions.new(options)
omit_background = options[:omit_background]
set_transparent_background_color if omit_background
result =
begin
timeout_helper.with_timeout do
@client.send_message('Page.printToPDF', pdf_options.page_print_args)
end
ensure
reset_default_background_color if omit_background
end
Puppeteer::ProtocolStreamReader.new(
client: @client,
handle: result['stream'],
).read_as_chunks
end
|
#default_navigation_timeout=(timeout) ⇒ Object
310
311
312
|
# File 'lib/puppeteer/page.rb', line 310
def default_navigation_timeout=(timeout)
@timeout_settings.default_navigation_timeout = timeout
end
|
#default_timeout=(timeout) ⇒ Object
315
316
317
|
# File 'lib/puppeteer/page.rb', line 315
def default_timeout=(timeout)
@timeout_settings.default_timeout = timeout
end
|
#delete_cookie(*cookies) ⇒ Object
392
393
394
395
396
397
398
399
|
# File 'lib/puppeteer/page.rb', line 392
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
|
#drag_interception_enabled=(enabled) ⇒ Object
295
296
297
298
|
# File 'lib/puppeteer/page.rb', line 295
def drag_interception_enabled=(enabled)
@user_drag_interception_enabled = enabled
@client.send_message('Input.setInterceptDrags', enabled: enabled)
end
|
#drag_interception_enabled? ⇒ Boolean
Also known as:
drag_interception_enabled
152
153
154
|
# File 'lib/puppeteer/page.rb', line 152
def drag_interception_enabled?
@user_drag_interception_enabled
end
|
#emulate(device) ⇒ Object
846
847
848
849
|
# File 'lib/puppeteer/page.rb', line 846
def emulate(device)
self.viewport = device.viewport
self.user_agent = device.user_agent
end
|
#emulate_cpu_throttling(factor) ⇒ Object
873
874
875
876
877
878
879
|
# File 'lib/puppeteer/page.rb', line 873
def emulate_cpu_throttling(factor)
if factor.nil? || factor >= 1
@client.send_message('Emulation.setCPUThrottlingRate', rate: factor || 1)
else
raise ArgumentError.new('Throttling rate should be greater or equal to 1')
end
end
|
#emulate_idle_state(is_user_active: nil, is_screen_unlocked: nil) ⇒ Object
926
927
928
929
930
931
932
933
934
935
936
937
|
# File 'lib/puppeteer/page.rb', line 926
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
|
882
883
884
885
886
887
888
889
890
891
892
893
894
|
# File 'lib/puppeteer/page.rb', line 882
def emulate_media_features(features)
if features.nil?
@client.send_message('Emulation.setEmulatedMedia', features: nil)
elsif features.is_a?(Array)
features.each do |media_feature|
name = media_feature[:name]
unless /^(?:prefers-(?:color-scheme|reduced-motion)|color-gamut)$/.match?(name)
raise ArgumentError.new("Unsupported media feature: #{name}")
end
end
@client.send_message('Emulation.setEmulatedMedia', features: features)
end
end
|
864
865
866
867
868
869
870
|
# File 'lib/puppeteer/page.rb', line 864
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_network_conditions(network_condition) ⇒ Object
305
306
307
|
# File 'lib/puppeteer/page.rb', line 305
def emulate_network_conditions(network_condition)
@frame_manager.network_manager.emulate_network_conditions(network_condition)
end
|
#emulate_timezone(timezone_id) ⇒ Object
897
898
899
900
901
902
903
904
905
|
# File 'lib/puppeteer/page.rb', line 897
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
|
#emulate_vision_deficiency(vision_deficiency_type) ⇒ Object
916
917
918
919
920
921
922
|
# File 'lib/puppeteer/page.rb', line 916
def emulate_vision_deficiency(vision_deficiency_type)
value = vision_deficiency_type || 'none'
unless VISION_DEFICIENCY_TYPES.include?(value)
raise ArgumentError.new("Unsupported vision deficiency: #{vision_deficiency_type}")
end
@client.send_message('Emulation.setEmulatedVisionDeficiency', type: value)
end
|
#eval_on_selector(selector, page_function, *args) ⇒ Object
Also known as:
Seval
360
361
362
|
# File 'lib/puppeteer/page.rb', line 360
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.
371
372
373
|
# File 'lib/puppeteer/page.rb', line 371
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<*>
951
952
953
|
# File 'lib/puppeteer/page.rb', line 951
def evaluate(page_function, *args)
main_frame.evaluate(page_function, *args)
end
|
#evaluate_handle(page_function, *args) ⇒ !Promise<!Puppeteer.JSHandle>
342
343
344
345
|
# File 'lib/puppeteer/page.rb', line 342
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
982
983
984
985
986
987
988
989
990
991
|
# File 'lib/puppeteer/page.rb', line 982
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
|
#expose_function(name, puppeteer_function) ⇒ Object
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
|
# File 'lib/puppeteer/page.rb', line 434
def expose_function(name, puppeteer_function)
if @page_bindings[name]
raise ArgumentError.new("Failed to add page binding with name `#{name}` already exists!")
end
@page_bindings[name] = puppeteer_function
add_page_binding = " function (type, bindingName) {\n /* Cast window to any here as we're about to add properties to it\n * via win[bindingName] which TypeScript doesn't like.\n */\n const win = window;\n const binding = win[bindingName];\n\n win[bindingName] = (...args) => {\n const me = window[bindingName];\n let callbacks = me.callbacks;\n if (!callbacks) {\n callbacks = new Map();\n me.callbacks = callbacks;\n }\n const seq = (me.lastSeq || 0) + 1;\n me.lastSeq = seq;\n const promise = new Promise((resolve, reject) =>\n callbacks.set(seq, { resolve, reject })\n );\n binding(JSON.stringify({ type, name: bindingName, seq, args }));\n return promise;\n };\n }\n JAVASCRIPT\n\n source = JavaScriptFunction.new(add_page_binding, ['exposedFun', name]).source\n @client.send_message('Runtime.addBinding', name: name)\n @client.send_message('Page.addScriptToEvaluateOnNewDocument', source: source)\n\n promises = @frame_manager.frames.map do |frame|\n frame.async_evaluate(\"() => \#{source}\")\n end\n await_all(*promises)\n\n nil\nend\n"
|
485
486
487
|
# File 'lib/puppeteer/page.rb', line 485
def ()
@frame_manager.network_manager. =
end
|
#focus(selector) ⇒ Object
1184
1185
1186
|
# File 'lib/puppeteer/page.rb', line 1184
def focus(selector)
main_frame.focus(selector)
end
|
#frames ⇒ Object
282
283
284
|
# File 'lib/puppeteer/page.rb', line 282
def frames
@frame_manager.frames
end
|
#geolocation=(geolocation) ⇒ Object
226
227
228
|
# File 'lib/puppeteer/page.rb', line 226
def geolocation=(geolocation)
@client.send_message('Emulation.setGeolocationOverride', geolocation.to_h)
end
|
#go_back(timeout: nil, wait_until: nil) ⇒ Object
819
820
821
|
# File 'lib/puppeteer/page.rb', line 819
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
825
826
827
|
# File 'lib/puppeteer/page.rb', line 825
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
648
649
650
|
# File 'lib/puppeteer/page.rb', line 648
def goto(url, referer: nil, timeout: nil, wait_until: nil)
main_frame.goto(url, referer: referer, timeout: timeout, wait_until: wait_until)
end
|
#handle_binding_called(event) ⇒ Object
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
|
# File 'lib/puppeteer/page.rb', line 536
def handle_binding_called(event)
execution_context_id = event['executionContextId']
payload =
begin
JSON.parse(event['payload'])
rescue
return
end
name = payload['name']
seq = payload['seq']
args = payload['args']
if payload['type'] != 'exposedFun' || !@page_bindings[name]
return
end
expression =
begin
result = @page_bindings[name].call(*args)
deliver_result = " function (name, seq, result) {\n window[name].callbacks.get(seq).resolve(result);\n window[name].callbacks.delete(seq);\n }\n JAVASCRIPT\n\n JavaScriptFunction.new(deliver_result, [name, seq, result]).source\n rescue => err\n deliver_error = <<~JAVASCRIPT\n function (name, seq, message) {\n const error = new Error(message);\n window[name].callbacks.get(seq).reject(error);\n window[name].callbacks.delete(seq);\n }\n JAVASCRIPT\n JavaScriptFunction.new(deliver_error, [name, seq, err.message]).source\n end\n\n @client.async_send_message('Runtime.evaluate', expression: expression, contextId: execution_context_id).rescue do |error|\n debug_puts(error)\n end\nend\n"
|
#handle_file_chooser(event) ⇒ Object
181
182
183
184
185
186
187
188
189
190
191
192
193
|
# File 'lib/puppeteer/page.rb', line 181
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
1191
1192
1193
|
# File 'lib/puppeteer/page.rb', line 1191
def hover(selector)
main_frame.hover(selector)
end
|
#init ⇒ Object
143
144
145
146
147
148
149
150
|
# File 'lib/puppeteer/page.rb', line 143
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
276
277
278
279
280
|
# File 'lib/puppeteer/page.rb', line 276
def keyboard(&block)
@keyboard.instance_eval(&block) unless block.nil?
@keyboard
end
|
#main_frame ⇒ Object
270
271
272
|
# File 'lib/puppeteer/page.rb', line 270
def main_frame
@frame_manager.main_frame
end
|
#metrics ⇒ Object
496
497
498
499
|
# File 'lib/puppeteer/page.rb', line 496
def metrics
response = @client.send_message('Performance.getMetrics')
Metrics.new(response['metrics'])
end
|
#offline_mode=(enabled) ⇒ Object
300
301
302
|
# File 'lib/puppeteer/page.rb', line 300
def offline_mode=(enabled)
@frame_manager.network_manager.offline_mode = enabled
end
|
#on(event_name, &block) ⇒ Object
158
159
160
161
162
163
164
165
166
167
168
169
170
|
# File 'lib/puppeteer/page.rb', line 158
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
if event_name.to_s == 'request'
super('request') do |req|
req.enqueue_intercept_action(-> { block.call(req) })
end
end
super(event_name.to_s, &block)
end
|
#once(event_name, &block) ⇒ Object
173
174
175
176
177
178
179
|
# File 'lib/puppeteer/page.rb', line 173
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
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
|
# File 'lib/puppeteer/page.rb', line 1119
def pdf(options = {})
chunks = create_pdf_stream(options)
StringIO.open do |stringio|
if options[:path]
File.open(options[:path], 'wb') do |f|
chunks.each do |chunk|
f.write(chunk)
stringio.write(chunk)
end
end
else
chunks.each do |chunk|
stringio.write(chunk)
end
end
stringio.string
end
rescue => err
if err.message.include?('PrintToPDF is not implemented')
raise PrintToPdfIsNotImplementedError.new
else
raise
end
end
|
#query_objects(prototype_handle) ⇒ !Promise<!Puppeteer.JSHandle>
351
352
353
354
|
# File 'lib/puppeteer/page.rb', line 351
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
322
323
324
|
# File 'lib/puppeteer/page.rb', line 322
def query_selector(selector)
main_frame.query_selector(selector)
end
|
#query_selector_all(selector) ⇒ !Promise<!Array<!Puppeteer.ElementHandle>>
Also known as:
SS
332
333
334
|
# File 'lib/puppeteer/page.rb', line 332
def query_selector_all(selector)
main_frame.query_selector_all(selector)
end
|
655
656
657
658
659
|
# File 'lib/puppeteer/page.rb', line 655
def reload(timeout: nil, wait_until: nil)
wait_for_navigation(timeout: timeout, wait_until: wait_until) do
@client.send_message('Page.reload')
end
end
|
#request_interception=(value) ⇒ Object
291
292
293
|
# File 'lib/puppeteer/page.rb', line 291
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
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
|
# File 'lib/puppeteer/page.rb', line 1010
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>>
1198
1199
1200
|
# File 'lib/puppeteer/page.rb', line 1198
def select(selector, *values)
main_frame.select(selector, *values)
end
|
#set_content(html, timeout: nil, wait_until: nil) ⇒ Object
635
636
637
|
# File 'lib/puppeteer/page.rb', line 635
def set_content(html, timeout: nil, wait_until: nil)
main_frame.set_content(html, timeout: timeout, wait_until: wait_until)
end
|
#set_cookie(*cookies) ⇒ Object
401
402
403
404
405
406
407
408
409
410
411
412
413
414
|
# File 'lib/puppeteer/page.rb', line 401
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
|
#set_user_agent(user_agent, user_agent_metadata = nil) ⇒ Object
Also known as:
user_agent=
491
492
493
|
# File 'lib/puppeteer/page.rb', line 491
def set_user_agent(user_agent, user_agent_metadata = nil)
@frame_manager.network_manager.set_user_agent(user_agent, user_agent_metadata)
end
|
‘$x()` in JavaScript. $ is not allowed to use as a method name in Ruby.
381
382
383
|
# File 'lib/puppeteer/page.rb', line 381
def Sx(expression)
main_frame.Sx(expression)
end
|
#tap(selector: nil, &block) ⇒ Object
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
|
# File 'lib/puppeteer/page.rb', line 1205
def tap(selector: nil, &block)
if selector.nil? && block
super(&block)
else
main_frame.tap(selector)
end
end
|
#title ⇒ String
999
1000
1001
|
# File 'lib/puppeteer/page.rb', line 999
def title
main_frame.title
end
|
#type_text(selector, text, delay: nil) ⇒ Object
1225
1226
1227
|
# File 'lib/puppeteer/page.rb', line 1225
def type_text(selector, text, delay: nil)
main_frame.type_text(selector, text, delay: delay)
end
|
#url ⇒ String
623
624
625
|
# File 'lib/puppeteer/page.rb', line 623
def url
main_frame.url
end
|
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
# File 'lib/puppeteer/page.rb', line 203
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_frame(url: nil, predicate: nil, timeout: nil) ⇒ Object
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
|
# File 'lib/puppeteer/page.rb', line 789
def wait_for_frame(url: nil, predicate: nil, timeout: nil)
if !url && !predicate
raise ArgumentError.new('url or predicate must be specified')
end
if predicate && !predicate.is_a?(Proc)
raise ArgumentError.new('predicate must be a proc.')
end
frame_predicate =
if url
-> (frame) { frame.url == url }
else
predicate
end
wait_for_frame_manager_event(
FrameManagerEmittedEvents::FrameAttached,
FrameManagerEmittedEvents::FrameNavigated,
predicate: frame_predicate,
timeout: timeout,
)
end
|
#wait_for_function(page_function, args: [], polling: nil, timeout: nil) ⇒ Puppeteer::JSHandle
1261
1262
1263
|
# File 'lib/puppeteer/page.rb', line 1261
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_navigation(timeout: nil, wait_until: nil) ⇒ Object
661
662
663
|
# File 'lib/puppeteer/page.rb', line 661
def wait_for_navigation(timeout: nil, wait_until: nil)
main_frame.send(:wait_for_navigation, timeout: timeout, wait_until: wait_until)
end
|
#wait_for_request(url: nil, predicate: nil, timeout: nil) ⇒ Object
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
|
# File 'lib/puppeteer/page.rb', line 729
def wait_for_request(url: nil, predicate: nil, timeout: nil)
if !url && !predicate
raise ArgumentError.new('url or predicate must be specified')
end
if predicate && !predicate.is_a?(Proc)
raise ArgumentError.new('predicate must be a proc.')
end
request_predicate =
if url
-> (request) { request.url == url }
else
predicate
end
wait_for_network_manager_event(NetworkManagerEmittedEvents::Request,
predicate: request_predicate,
timeout: timeout,
)
end
|
#wait_for_response(url: nil, predicate: nil, timeout: nil) ⇒ Object
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
|
# File 'lib/puppeteer/page.rb', line 763
def wait_for_response(url: nil, predicate: nil, timeout: nil)
if !url && !predicate
raise ArgumentError.new('url or predicate must be specified')
end
if predicate && !predicate.is_a?(Proc)
raise ArgumentError.new('predicate must be a proc.')
end
response_predicate =
if url
-> (response) { response.url == url }
else
predicate
end
wait_for_network_manager_event(NetworkManagerEmittedEvents::Response,
predicate: response_predicate,
timeout: timeout,
)
end
|
#wait_for_selector(selector, visible: nil, hidden: nil, timeout: nil) ⇒ Object
1235
1236
1237
|
# File 'lib/puppeteer/page.rb', line 1235
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
1242
1243
1244
|
# File 'lib/puppeteer/page.rb', line 1242
def wait_for_timeout(milliseconds)
main_frame.wait_for_timeout(milliseconds)
end
|
#wait_for_xpath(xpath, visible: nil, hidden: nil, timeout: nil) ⇒ Object
1250
1251
1252
|
# File 'lib/puppeteer/page.rb', line 1250
def wait_for_xpath(xpath, visible: nil, hidden: nil, timeout: nil)
main_frame.wait_for_xpath(xpath, visible: visible, hidden: hidden, timeout: timeout)
end
|
#workers ⇒ Object
286
287
288
|
# File 'lib/puppeteer/page.rb', line 286
def workers
@workers.values
end
|