//Input the file path into the filename field:
SendKeys.SendWait(longfilepath);
//Input "Enter" key
SendKeys.SendWait(@"{Enter}");
https://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.sendwait(v=vs.110).aspx
drv.FindElement(By.XPath("//input[@type='file']")).SendKeys("ABC");
附注:如果页面上有多个文件上传输入字段,则可能需要更具体的 XPath
,例如 "//input[@id='some_specific_file_upload']"
...
HTML
。只需右键单击所需的网页元素,然后选择类似于“检查元素”的选项即可。 - Andersson
ActiveElement()
返回页面中的活动元素而不是文件对话框。Selenium 不支持文件对话框,但它支持通过在<input type="file">
元素上直接调用Sendkeys
来上传文件。 - Florent B.