3

I have a WebView node in my JavaFX and the page loaded in it has a <input type=file>. I want to set this input's value to a file on disk. I know this is not possible by injecting javascript into the web view. But I'm wondering if I can get access to JavaFX internals in how these input fields are handled and set the value through there. There appears to be no mention of file input controls' handling in the docs so I'm lost on this.

When I click on the file input. JavaFX gives me a native file selector. So, I'm expecting there's some form of handler that is invoked when clicking on a file input, that asks the user to select a file and then fills the file input with this value. That's what I want to do.

I tried just getting the element and setting it's value, but of course, it didn't work.

webEngine.getDocument().getElementById("FileInput")
    .setNodeValue("C:\\attachment.pdf");

This piece of code does nothing. No error, no result either.

So, any ideas?

sharat87
  • 7,330
  • 12
  • 55
  • 80
  • It's a security restriction: see [How to set a value to a file input in HTML?](http://stackoverflow.com/questions/1696877/how-to-set-a-value-to-a-file-input-in-html) - in other words: not possible. – fireandfuel Nov 27 '16 at 00:05
  • It's a security restriction to do this within the browser with javascript. Not when I have full control of the browser component. I'm not asking to be able to do this from javascript. I want to do this from java. – sharat87 Nov 27 '16 at 03:47

1 Answers1

1

JavaFX internally uses WebKit for the WebView node, thus it have the same security restrictions. It's not possible to set the value of <input type="file"/> programmatically, neither over JavaScript or Java.

I suggest you to use FileChooser without using the WebView node. What the WebView node shows is a select button, which calls a FileChooser, and a label with the selected file name. This can be easily implemented in Java source code using JavaFX.

fireandfuel
  • 732
  • 1
  • 9
  • 22
  • Thanks for the input but I think you misunderstood. I'm loading a webpage so I need `WebView`. I'm not after just any file chooser, I need to put a file in the file input of a webpage. And I don't believe it to not be possible, especially from Java. Host systems tend to have full control over the internals of webkit when it is embedded. For example, phantomjs has a function `uploadFile` that does exactly this. – sharat87 Nov 27 '16 at 15:31
  • @ShrikantSharat It's not possible with pure JavaFX to take full control over WebKit. JavaFX is a relatively young GUI framework - compared to Swing and AWT. --- PS: It sounds like you are trying to implement something like a webpage automation. Then have a look at so called `WebDrivers`, like https://github.com/SeleniumHQ/selenium or https://github.com/lukas-krecan/javafx-webview-webdriver . – fireandfuel Nov 27 '16 at 15:45
  • I appreciate JavaFX being young, I was wondering if this is already supported. You're right. I am doing browser automation but the problem with web drivers for what I know is they can't run in the background. For instance, in JavaFX, I can do the automation in web view and once my automation is all set, I can run the java app without *show*ing the window. So, the automation runs without displaying any GUI window. – sharat87 Nov 27 '16 at 17:24