Uploading Files Not Working - trim the initial characters

你说的曾经没有我的故事 提交于 2019-12-12 02:12:41

问题


I am trying to upload a file using the WebBrowser control. It trims the starting character sometimes one and sometimes three then choose window gives error Invalid file name!. Can't seem to do it and need some help.

Here is the Html:

    <input name="UploadedFile" id="UploadedFile" type="file" />
    <input name="up" id="up" type="button" value="Upload" />

Here is the vb code:

Dim el = elc.GetElementsByName("UploadedFile")
el.Item("UploadedFile").Focus()
'    SendKeys.Send("Capture.png" & "{ENTER}")
SendKeys.Send("C:\Capture.png" + "{ENTER}")
el.Item("UploadedFile").InvokeMember("Click")

that the file upload button comes up and hit enter, but can't input full filename into the file name area.

If I use thisSendKeys.Send("C:\Capture.png" + "{ENTER}"). It gives this error: Choose window error screenshot

If I use this SendKeys.Send("Capture.png" + "{ENTER}"). It gives this error: Choose window error screenshot

And if I put extra character then it works fine but it doesn't always trim one character so I can't put an extra character to solve this error.


回答1:


The problem might be that the sendkeys comes up too fast behind focus, so the first few characters don't get picked up. Try just filling the textbox all at once with one line by setting the textbox's value rather than trying to imitate user keystrokes, this could replace both the focus and sendkeys lines:

WebBrowser1.Document.GetElementById("UploadedFile").SetAttribute("value", "C:\Capture.png")

...and then call the button-click




回答2:


You are right @soohoonigan the sendkeys comes up too fast but that is not an answer for that. I did this like that.

Here is my code:

Dim el = elc.GetElementsByName("UploadedFile")
SetFile()
el.Item("UploadedFile").InvokeMember("Click")

Public Async Sub SetFile()

       Await Task.Delay(1000)
       SendKeys.Send("c:\Capture.png" & "{ENTER}")

End Sub

It's working fine.



来源:https://stackoverflow.com/questions/39454859/uploading-files-not-working-trim-the-initial-characters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!