PowerShell:在input type = file Form中写入值。

时间:2010-02-04 10:23:16

标签: html internet-explorer powershell file-upload input

我在网页上有一个输入类型=文字输入框我加载并填充值,然后点击提交按钮,工作正常:

$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填充输入类型=文件框并上传它。 我读到,由于安全原因,浏览器不支持value =。

使用PowerShell执行此操作是否有任何解决方法? 也许“点击”浏览按钮并选择文件或使用sendkeys?

3 个答案:

答案 0 :(得分:4)

检查Jaykul's post。他使用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' })

$f = $ie.FileUploads | ? { $_.GetAttributeValue("name") -eq 'the name you have' }

答案 1 :(得分:1)

我强烈考虑使用WebClientHttpWebRequest而不是驱动IE GUI。使用PowerShell有一个WebClient example

答案 2 :(得分:0)

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