《SeleniumBasic 3.141.0.0 - 在VBA中操作浏览器》高级技术之十二:自动选择文件并上传


在浏览器中打开如下网址: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以后,可以看到提示“上传文件成功”

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM