Uploading Files Not Working - trim the initial cha

2019-09-05 17:34发布

问题:

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.