首页 > 解决方案 > 通过 Powershell 上传 HTTP 文件

问题描述

我尝试通过 Fritz!Box 上的 powershell 自动上传文件,但它不起作用。我通过 Internet Explorer 登录它。

$ie = New-Object -com InternetExplorer.Application 
$ie.visible=$true
$ie.Navigate("192.168.178.1/")
do {sleep 1} until (-not ($ie.Busy))
$ie.document.getElementById("uiPass").value = "pass"
$ie.Document.getElementById("submitLoginBtn").click()
do {sleep 1} until (-not ($ie.Busy)) 
$ie.Document.getElementsByTagName['uiImport'].value=Get-Content "myfile" -Raw

我的问题是,我真的不知道 uiImport 的属性是什么。这是我的文件应该以表格形式上传的地方,但 Powershell 一直说它找不到这个属性。

标签: powershellhttpinternet-explorer

解决方案


$ie.Document.getElementsByTagName['uiImport'].value=Get-Content "myfile" -Raw

请尝试使用F12开发者工具检查网页资源,页面是否包含“uiImport”自定义标签。

从我的角度来看,我认为也许“uiImport”是输入元素的名称属性,而不是标签名称。如果是这种情况,您可以使用Document.getElementsByName()方法来查找输入文本。然后,设置值。

示例代码如下:

$ie = New-Object -com InternetExplorer.Application 
$ie.visible=$true
$ie.Navigate("<web page url>")
do {sleep 1} until (-not ($ie.Busy))

$elements = $ie.document.getElementsByName("files")
$elements[0].value ="file path"

网页资源:

Upload file : <input type="text" id="txtupload" name="files" class="file"  value=""/><br />

Download file : <input type="text" id="txtdownload" name="files" class="file" value=""/>

在上面的示例中,我使用索引来查找特殊的上传文本,您也可以使用 where 子句或使用 Document.getElementById() 方法来查找文本框。代码如下:

$uploadtext = $ie.document.getElementsByName("files") | ?{ $_.Id -eq 'txtupload'}
$uploadtext.value ="hello world"
$downloadtext = $ie.document.getElementsByName("files") | where{ $_.Id -eq 'txtdownload'}
$downloadtext.value ="hi"

推荐阅读