YusukeIwaki / puppeteer-ruby

A Ruby port of Puppeteer
Apache License 2.0
290 stars 41 forks source link

screenshots with full_page: false are returning full page screenshots #298

Closed kyledrake closed 1 year ago

kyledrake commented 1 year ago
require 'puppeteer'

Puppeteer.launch(headless: false) do |browser|
  page = browser.pages.first || browser.new_page
  page.viewport = Puppeteer::Viewport.new(width: 1280, height: 800)
  page.goto("https://github.com/YusukeIwaki")
  page.screenshot(path: "1.capture_a_site.png", full_page: false)
end

Expected behavior is image that's cropped to the browser viewport, instead it returns a full page screenshot:

1 capture_a_site

google-chrome-stable                          109.0.5414.74-1                         amd64
ruby 3.1.3p185 (2022-11-24 revision 1a6b16756e) [x86_64-linux]
kyledrake commented 1 year ago

Possible workaround is to use clip:

require 'puppeteer'

Puppeteer.launch(headless: true) do |browser|
  page = browser.pages.first || browser.new_page
  page.viewport = Puppeteer::Viewport.new(width: 1280, height: 800)
  page.goto("https://github.com/YusukeIwaki")
  page.screenshot(path: "1.capture_a_site.png", clip: {x: 0, y: 0, width: 1280, height: 800})
end
YusukeIwaki commented 1 year ago

Current version of puppeteer-ruby issues a CDP command Page.captureScreenshot with an option captureBeyondViewport: true unexpectedly.

$ DEBUG=1 bundle exec ruby capture.rb | grep capture
D, [2023-01-19T01:43:47.390049 #88713] DEBUG -- : SEND >> {"sessionId":"F2B6134C8553CF72D74712F9E7B32669","method":"Page.captureScreenshot","params":{"format":"png","captureBeyondViewport":true},"id":18}

Older version didn't.

$ DEBUG=1 bundle exec ruby capture.rb | grep capture
D, [2023-01-19T01:43:10.974871 #88618] DEBUG -- : SEND >> {"sessionId":"92E2BEA571CD5774E4045ADDB145E4BC","method":"Page.captureScreenshot","params":{"format":"png"},"id":17}
YusukeIwaki commented 1 year ago

@kyledrake Sorry for inconvenience, puppeteer-ruby 0.45.1 would fix this issue. Please try it.