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?
lon2 last edited by lon2
@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())
lon2 last edited by lon2
@lon2, if your goal is to automate uploading a file to a website, I would first check the page source with
requests.getto see if it is a straight-forward submit form, then just
@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.
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.
@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 last edited by
@lon2 when you do
v = WKWebView()
v is an ui.View but v.webview is an ObjectiveC WKWebView
@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.
cvp last edited by cvp
JonB last edited by
@lon2 can you post the full traceback?
Also, what is the website you are trying to use? Is the file input in a iframe?