Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Pick file from page in ui.WebView
-
@lon2, can you share a minimal example of your code? How you create the ui.WebView, and which page/content you are accessing, is it using JS or regular HTML for the file downloader?
-
@mikael I created the ui.WebView by putting it in a gui and referencing it from the code (name = 'webview').
I don't use JS in this example.
I'm using iOS 10.3. An example code is this:#!python2 # coding: utf-8 import ui,requests, json, time, console, urllib, photos v = ui.load_view('dialog') v.present(style='full_screen') w=v['webview'] #ANY SITE THAT CONTAINS THE BUTTON TO UPLOAD A FILE w.load_url('http://www.htmldog.com/examples/inputfile/')
Thank you :)
PS: the menu to select a photo from the camera roll appears, but clicking on one of the options closes the gui.
-
@lon2, thanks for taking the time to share the sample. One more thing: have you allowed Pythonista to access your photos?
(And do you have a special reason to use Python2?)
-
@lon2, ok, with this minimal example, I get the same behavior: script closes with no traceback when I move to my photo library.
# coding: utf-8 import ui w = ui.WebView() w.present('fullscreen') #ANY SITE THAT CONTAINS THE BUTTON TO UPLOAD A FILE w.load_url('http://www.htmldog.com/examples/inputfile/')
-
@mikael -- same behavior with your wkwebview?
Edit:. Seems you have to implement a UIDelegate:
-
@lon2, not sure what you want to accomplish, but one option is to use the Safari view:
from objc_util import * SFSafariViewController = ObjCClass('SFSafariViewController') @on_main_thread def open_in_safari_vc(url, tint_color=None): vc = SFSafariViewController.alloc().initWithURL_(nsurl(url)) if tint_color is not None: vc.view().tintColor = tint_color app = UIApplication.sharedApplication() root_vc = app.keyWindow().rootViewController() root_vc.presentViewController_animated_completion_(vc, True, None) vc.release() if __name__ == '__main__': open_in_safari_vc('http://www.htmldog.com/examples/inputfile/', UIColor.orangeColor())
-
@mikael my purpose was to automate the ui.webView using Javascript. I did this by inserting the ui.webView into a gui. You cannot set an input file field of an html page via JS so I thought of manually selecting the file by clicking on the upload button on the page. The code you sent me opens a view in Safari but I don't think I can automate it by putting it in another gui and then referencing it, right? ..there would be no problem if you can somehow automatically pick a file from the camera roll for the file input field of the html page.
-
@lon2, if your goal is to automate uploading a file to a website, I would first check the page source with
requests.get
to see if it is a straight-forward submit form, then justrequests.post
the image. -
@mikael The upload of the photo to the page I want is not so easy to perform: there are dynamic and hidden parameters in the form, the url of the post request is not exposed, etc.. For these reasons, I had thought of automating the webview after having uploaded the photo manually ..but in this case I can't choose the photo from the iPhone gallery.
-
You can also execute javascript within the webview, which might be easier.
Edit — oh, I see you tried. have you tried reading the file, converting it to a data url, and then setting the form field to the data url? I thought that’s how file input types work under the hood, though I haven’t tried.
-
Hmm, on ipadOs 13.7, @mikael’s example works fine. Maybe they finally fully depreciated uiwebview in iOS 14?
Have you tried wkwebview?
https://github.com/mikaelho/pythonista-webview -
@JonB the example you mentioned works for me too, but in that case I can't automate the Safari web view. I tried to use the wkwebview but I get the error 'wkwebview object has no attribute objc_instance' when running the script .. maybe because it is not compatible with the version of the python interpreter 3.5
-
-
@cvp In my version I can't instantiate the wkwebview due to the error that occurred and that I wrote in my previous post. I will try to solve the initial problem in another way if possible.
-
-
@lon2 can you post the full traceback?
Also, what is the website you are trying to use? Is the file input in a iframe?
I think there may be a few methods to use javascript to populate an input. The example site above uses iframes, and we views seem to have cross site scripting protections that prevent you from running JavaScript in iframes.
Also, it might be easier to use javascript to extract the action url, and populate the request directly, if there is a specific site you are targeting. Even if you can't find the url in the source, the browser obviously needs to know what it is, and it will be accessible via JavaScript.
By the way, in @mikael's webview, you can use the console() method to run a JavaScript console that is useful for debugging. (See the pull request to fix a bug on the JS logging script)