I am trying to upload image file into the browser using mechanize. Although there is no error, the uploaded file does not reflect when I check manually in the browser (post submit/saving). I am using the following code to upload the files
import mechanize as mc
br = mc.Browser()
br.set_handle_robots(False)
br.select_form(nr=0)
br.form.add_file(open("test.png"), content_type="image/png",
filename='before',name="ctl00$ContentPlaceHolder1$fileuploadBeforeimages")
br.submit("ctl00$ContentPlaceHolder1$cmdSave")
# this is supposed to save the form on the webpage. It saves the texts in the other fields, whereas the image does not show up.
The add file command seems to work. I can confirm this because when I print br.forms()[0]
the file details show up (<FileControl(ctl00$ContentPlaceHolder1$fileuploadBeforeimages=before)>
).
But there is no sign of the image file post this code snippet. I have checked several examples which include br.submit()
without any specific button control, when I do this no page is saved on the website.
What am I missing?
Thanks in advance.
EDIT
When I manually try to upload the file, I see a pop-up asking for confirmation. Under inspect, this is present as
onchange="if (confirm('Upload ' + this.value + '?')) this.form.submit();"
I am not sure if this is a JavaScript
element and mechanize cannot pass through this part for upload function. Can someone confirm this.?