Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Screenshot of a screen portion. #285

Closed
mathieujobin opened this issue Sep 8, 2022 · 11 comments
Closed

Screenshot of a screen portion. #285

mathieujobin opened this issue Sep 8, 2022 · 11 comments

Comments

@mathieujobin
Copy link

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
Copy link

zsan commented Sep 8, 2022

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
Copy link
Author

mathieujobin commented Sep 8, 2022

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
Copy link
Contributor

ttilberg commented Sep 8, 2022

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

@mathieujobin
Copy link
Author

mathieujobin commented Sep 9, 2022

🤦‍♂️ 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
Copy link
Member

route commented Sep 11, 2022

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

@zsan
Copy link

zsan commented Feb 1, 2023

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
Copy link

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

@route
Copy link
Member

route commented Oct 27, 2023

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

@alexeyr-ci
Copy link

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
Copy link

I think this was fixed in #410

Close?

@alexeyr-ci
Copy link

Yes, looks like it.

@route route closed this as completed Nov 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants