我有一个用于在 Internet Explorer 中自动执行任务的脚本。但是该脚本只有在第一次失败后才有效。
如果我打开powershell.exe
并运行脚本,它会给我错误
无法在空值表达式上调用方法。
但如果我F5再次点击并运行它,它从那时起就可以完美运行。如果我关闭 PowerShell 并重新打开,错误会在第一次尝试时再次出现。
脚本的逻辑基本上是这样的
$doc = $ie.Document;
$frames = @($doc.getElementsByTagName("FRAME"));
foreach ($frame in $frames) {
try {
$inputs = @($frame.ContentWindow.document.getElementsByTagName("INPUT"));
foreach ($button in $inputs |
where {$_.type -eq "button" -and $_.value -eq "Ny sag"}) {
$button.click(); $x = 11;
}
} catch {
Exception -errtxt $error[0] -custom_txt ("attempt # " + [string]$x);
}
}
代码失败上线
$inputs = @($frame.ContentWindow.document.getElementsByTagName("INPUT"));
我尝试添加默认的 webclient,但没有任何改变。
$wc = New-Object System.Net.WebClient;
$wc.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials;