rubycdp / ferrum

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

Upload/Attach File on button Click #284

Closed wonderer007 closed 2 years ago

wonderer007 commented 2 years ago

Can we upload/attach file on a button click ?

I think https://github.com/rubycdp/ferrum#select_file might be what I am looking for but its missing documentation.

I am trying this but

button = browser.at_xpath("//button[text() = 'Select File']")
button.select_file(Rails.root.join("file.png"))

It return error

Ferrum::BrowserError: Node is not a file input element

Thank you so much for this beautiful library

ttilberg commented 2 years ago

You are calling the method correctly, but the node you are calling it on should be an <input> element, not a button. Does the page you’re interacting with have a <form> element with a file <input> value?

wonderer007 commented 2 years ago

Yes, page has <form> element and a file <input type="text"> element inside it. This form is present inside a pop up/dialog. I am able to open the pop with the click though. But not able to attach file with it. Now tried this

browser.at_xpath("//input").select_file(Rails.root.join("file.png").to_s)

and it returns

{}

But, nothing happens on the the pop up/dialog.

Normal flow is,

  1. click on button (it opens a pop up/dialog)
  2. click on upload (it opens file selector)
  3. choose file and it shows preview
wonderer007 commented 2 years ago

@ttilberg can we have drag/drop file ?

ttilberg commented 2 years ago

I’m not a great person to ask about capabilities, @route is best. However, I suspect he won’t have much to add. It looks like this is an unsolved issue in the Puppeteer world as well, which is a much more mature library with many more people working on it. Being that both Puppeteer and Ferrum are interfaces for Chrome Devtools Protocol, you can usually check Puppeteer for potential solutions you can use in Ferrum to get a hint. In this case, it doesn’t look straight forward.

wonderer007 commented 2 years ago

Actually calling click on file input element solved the issue @ttilberg thank you so much!