在瀏覽器中打開如下網址:https://www.kj009.net/web1/upload.aspx
可以看到一個“選擇文件”的按鈕,其實它的定義是一個input,只不過type是file。
<input name="myfile" type="file" id="myfile">
如果手工操作的話,會彈出一個選擇文件的對話框。在SeleniumBasic中,只需要定位到該元素,然后利用SendKeys方法把要上傳的文件路徑設置上就行。
Dim file As SeleniumBasic.IWebElement Dim Button1 As SeleniumBasic.IWebElement WD.url = "https://www.kj009.net/web1/upload.aspx" Set file = WD.FindElementById("myfile") If file Is Nothing = False Then file.SendKeys "D:\Temp\VBE2014_SourceCode.rar" End If Set Button1 = WD.FindElementById("Button1") Button1.Click
運行完Sendkeys那句以后,看到右側已經顯示文件名稱了。
代碼中的Button1,是“我確認文件符合要求,現在上傳”這個按鈕。Click以后,可以看到提示“上傳文件成功”