我尝试了很多方法来做到这一点,但这是不可能的,因为 IE8 出于安全原因将 Value 属性设置为只读。
好吧,我确实找到了解决办法。它非常简单,但使用SendKeys
并不总是被认为是最好的编程解决方案。但是,它完成了工作。
oBrowser.Navigate("http://www.some-url.com", ref oNull, ref oNull, ref oNull, ref oNull);
while (oBrowser.Busy)
{
System.Threading.Thread.Sleep(20);
}
//Locate the Input element of type="file"
HTMLInputElement file_1 = (HTMLInputElement)oDoc.all.item("file_1", 0);
file_1.select();
SendKeys.Send("{TAB}"); //Navigate to the Browse button
SendKeys.Send(" "); //Click on the browse button
SendKeys.Send(@"c:\Test.txt"); //File Path
SendKeys.Send("{TAB}");
SendKeys.Send("{ENTER}");
SendKeys
通过检查活动窗口并可能在每个命令之前设置正确的窗口,可以改进此代码。这可以通过使用FindWindow()
and FindWindowEx()
of来完成user32.dll
。windows API 中的这些方法用于查找活动窗口和子窗口。