English 中文(简体)
PowerShell:在input type=file表单中写入值。
原标题:PowerShell: write value in a input type=file Form.

我在加载的网页上有一个 input type=text 输入框,我填充值后点击提交按钮,一切都正常。

$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}

I d also like to populate a input type=file box with c: emp est.txt and upload this. I read that because of security reasons the value= is not supported from the browsers.

Is there any workaround to do this with PowerShell? Maybe "click" the browse button and select the file or use sendkeys?

最佳回答

检查Jaykul的文章。他使用Watin进行自动化。只需下载程序集并尝试即可。我可以像这样设置值,然后提交表单:

$WatinPath =  c:inwatinWatiN.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:	emp	est.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和类似类的原因,但是如果可能的话,请在其他情况下使用它们。

编辑:

如果你没有元素的身份证,但仅知道名称,你可以尝试

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

或者

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

我强烈建议使用WebClient或HttpWebRequest而不是驱动IE GUI。有一个使用PowerShell的WebClient示例。

是的,如果你想在IE中做到这一点,就必须使用SendKeys路线。在IE8及以上版本中,直接文本输入框将被阻止。





相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签