rubycdp / ferrum

Headless Chrome Ruby API
https://ferrum.rubycdp.com
MIT License
1.7k stars 122 forks source link

Screenshot of a screen portion. #285

Closed mathieujobin closed 9 months ago

mathieujobin commented 1 year ago

I wish to be able to use ferrum to load a webpage and take a screenshot of a specific screen portion.

it should be delimited by x,y,w,h or css selector.

I tried....

a = browser.position = { left: 80, top: 500 } ; browser.screenshot(path: "xe-test-1.jpg")
b = browser.screenshot(path: "xe-test-1.jpg", selector: "div#undefined")
c = browser.at_css("div#undefined").screenshot(path: "xe-test-1.jpg")

none works.

zsan commented 1 year ago

option selector is worked for me

 browser.screenshot(selector: "#CaptchaImage", path: "captcha.png")

Version:

  bundle show ferrum
/Users/zak/.rbenv/versions/2.7.6/lib/ruby/gems/2.7.0/gems/ferrum-0.11
mathieujobin commented 1 year ago

ok, how about this error...

2.7.6 :021 > browser = Ferrum::Browser.new
 => 
#<Ferrum::Browser:0x00007fa3059957f0
... 
2.7.6 :022 > browser.go_to "https://www.xe.com/currencycharts/?from=USD&to=JPY&view=1W"
 => "621A54522E64E21B4459AF7DE4057BB2" 

2.7.6 :023 > browser.screenshot(path: "xe-test-2.jpg", selector: "dev section div div main div")
Traceback (most recent call last):
        31: from /home/mathieu/.rvm/gems/ruby-2.7.6/bin/ruby_executable_hooks:22:in `<main>'                         
        30: from /home/mathieu/.rvm/gems/ruby-2.7.6/bin/ruby_executable_hooks:22:in `eval'                        
        29: from /home/mathieu/.rvm/gems/ruby-2.7.6/bin/irb:23:in `<main>'                                        
        28: from /home/mathieu/.rvm/gems/ruby-2.7.6/bin/irb:23:in `load'                                          
        27: from /home/mathieu/.rvm/gems/ruby-2.7.6/gems/irb-1.4.1/exe/irb:11:in `<top (required)>'               
        10: from (irb):20:in `<main>'                                                                             
         9: from /home/mathieu/.rvm/rubies/ruby-2.7.6/lib/ruby/2.7.0/forwardable.rb:235:in `screenshot'           
         8: from /home/mathieu/.rvm/gems/ruby-2.7.6/gems/ferrum-0.11/lib/ferrum/page/screenshot.rb:33:in `screenshot'
         7: from /home/mathieu/.rvm/gems/ruby-2.7.6/gems/ferrum-0.11/lib/ferrum/page/screenshot.rb:143:in `screenshot_options'
         6: from /home/mathieu/.rvm/gems/ruby-2.7.6/gems/ferrum-0.11/lib/ferrum/page/screenshot.rb:159:in `get_bounding_rect'
         5: from /home/mathieu/.rvm/rubies/ruby-2.7.6/lib/ruby/2.7.0/forwardable.rb:235:in `evaluate_async'
         4: from /home/mathieu/.rvm/gems/ruby-2.7.6/gems/ferrum-0.11/lib/ferrum/frame/runtime.rb:70:in `evaluate_async'
         3: from /home/mathieu/.rvm/gems/ruby-2.7.6/gems/ferrum-0.11/lib/ferrum/frame/runtime.rb:124:in `call'
         2: from /home/mathieu/.rvm/gems/ruby-2.7.6/gems/ferrum-0.11/lib/ferrum.rb:145:in `with_attempts'
         1: from /home/mathieu/.rvm/gems/ruby-2.7.6/gems/ferrum-0.11/lib/ferrum/frame/runtime.rb:141:in `block in call'
/home/mathieu/.rvm/gems/ruby-2.7.6/gems/ferrum-0.11/lib/ferrum/frame/runtime.rb:157:in `handle_error': TypeError: Cannot read properties of null (reading 'getBoundingClientRect') (Ferrum::JavaScriptError)
    at <anonymous>:10:26
    at new Promise (<anonymous>)
    at <anonymous>:2:10
ttilberg commented 1 year ago

Might be a case of not-obvious error message. Are you sure that selector exists? Notably the first path, dev vs div?

mathieujobin commented 1 year ago

:man_facepalming: OMG.... so it works, but still need to scroll to that section... xe-test-2

similar result with

browser.screenshot(path: "xe-test-2.jpg", selector: "div section div div main > div")
browser.screenshot(path: "xe-test-2.jpg", selector: "div section div div main:nth-child(2) > div:nth-child(2) > div:nth-child(2)")

selector appears correct but bottom is all blank

route commented 1 year ago

I think we could try to pass area arguments right to the method which now is not supported but can be

zsan commented 1 year ago

I think we could try to pass area arguments right to the method which now is not supported but can be

That would be cool @route

alexeyr-ci commented 10 months ago

The Page.captureScreenshot command has a clip parameter, it could be passed from options.

route commented 10 months ago

@alexeyr-ci we just need to find either time for me or someone who can implement it ;)

alexeyr-ci commented 10 months ago

I'd make a PR, but I ended up just calling Page.captureScreenshot directly as a workaround :) I still might end up doing that. Also I missed that you meant that already in

I think we could try to pass area arguments right to the method which now is not supported but can be

As a fix for

selector appears correct but bottom is all blank

which I also ran into I think you just need to intersect the bounding rectangle in area_options with the viewport (when full is false).

sandstrom commented 9 months ago

I think this was fixed in https://github.com/rubycdp/ferrum/pull/410

Close?

alexeyr-ci commented 9 months ago

Yes, looks like it.