Html PowerShell:以输入类型=文件形式写入值。

Html PowerShell:以输入类型=文件形式写入值。,html,internet-explorer,powershell,file-upload,input,Html,Internet Explorer,Powershell,File Upload,Input,我在网页上有一个input type=textinputboxs,我加载并填充值,然后单击submit按钮,效果很好: $ie=New-Object -comobject InternetExplorer.Application $ie.visible=$true $ie.Navigate("https://myurl/test.html") while($ie.busy){Start-Sleep 1} $ie.Document.getElementById("field_firstNa

我在网页上有一个input type=textinputboxs,我加载并填充值,然后单击submit按钮,效果很好:

$ie=New-Object -comobject InternetExplorer.Application 
$ie.visible=$true 
$ie.Navigate("https://myurl/test.html") 
while($ie.busy){Start-Sleep 1} 
$ie.Document.getElementById("field_firstName").value="Firstname" 
$ie.Document.getElementById("field_lastName").value="Lastname" 
$ie.Document.getElementById("btn_upload").Click() 
while($ie.busy){Start-Sleep 1}
我还想用c:\temp\test.txt填充一个input type=file框,并上传这个文件。 我了解到,由于安全原因,浏览器不支持value=

使用PowerShell是否有解决方法?
也许“单击”浏览按钮并选择文件或使用sendkeys

>我强烈建议使用或代替驱动IE GUI。存在正在使用PowerShell的问题。

请检查。他用Watin实现自动化。只需下载程序集并重试。我可以设置值,然后像这样提交表单:

$WatinPath = 'c:\bin\watin\WatiN.Core.dll' #path with downloaded assembly
$watin     = [Reflection.Assembly]::LoadFrom( $WatinPath )

$ie        = new-object WatiN.Core.IE("https://myurl/test.html")
$file1 = $ie.FileUpload('file1') #id of the input
$file1.set('C:\temp\test.txt') # path to the file

# and now just find the button and click on it
$o = $ie.Button('send') #send is id of the submit button
$o.Click()
我理解您使用IE而不是
WebClient
和类似类的原因,但如果可能,请在其他情况下使用它们

编辑:

如果您没有元素的ID,但只有名称,您可以尝试

$f = $ie.FileUpload({param($fu) $fu.GetAttributeValue("name") -eq 'the name you have' })


是的,如果您想在IE中执行此操作,您必须使用SendKeys路由。IE8及更高版本会阻止直接文本输入框。

谢谢您的回复,Matthew。虽然我现在更喜欢IE Gui,因为我可以看到结果,避免代理证书等问题。。我很想知道上面发布的代码在使用webclient方法时会是什么样子。看起来你可能无法用纯webclient做你想做的事情。CodeProject()中有一个函数可以查看。它基本上是基于HttpWebRequest的WebClient.UploadFile的一个更复杂的版本。这看起来不错,尽管我在找到正确的字段时遇到了问题,因为源代码没有id,只有一个名称。。。你知道我是否可以按名称而不是id进行搜索吗?我使用了你的示例,你调用函数getElementById,所以我认为你知道id。我会检查是否可能。我也很困惑为什么我的第一个示例有效,因为在那里我也只有一个名称而没有id标记。。。在上一个示例$\ GetAttributeValue(“名称”)中,我似乎找到了该框,但我的submitbutton(也没有id,只有名称)不起作用。明天再玩一次。非常感谢您,您必须找到与文件上载类似的提交按钮:
$sub=$ie.button({param($fu)$t.GetAttributeValue(“name”)-eq‘提交按钮的名称’})
我不太清楚,现在无法检查它。只要试试
$ie | gm
,你就会找到正确的方法;)这成功了!谢谢在watin列表中,我找到了:ie.FileUpload(Find.ByName(“ctl02$myfile”))).Set(“test.txt”),但我无法让Find.ByName正常工作。我现在向您推荐解决方案。
$f = $ie.FileUploads | ? { $_.GetAttributeValue("name") -eq 'the name you have' }