Html 使用 powershell 单击网页上的链接,然后传递凭据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10436332/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 00:08:14  来源:igfitidea点击:

using powershell to click a link on a web page and then pass credentials

htmlpowershellautomated-tests

提问by digitalslavery

I am trying to test our a web page we built that has a temp ssl cert, the issue is that when I goto the page I get the IE security warning that the ssl cert is invalid, with two links one to close the page, and the other to proceed to the page. I was able to use powershell to open IE and click the link on the ssl warning page, but now I need to populate the username and password input boxes and then click the login button.

我正在尝试测试我们构建的具有临时 ssl 证书的网页,问题是当我转到该页面时,我收到 IE 安全警告,即 ssl 证书无效,有两个链接一个关闭页面,以及另一个进入页面。我能够使用powershell打开IE并单击ssl警告页面上的链接,但现在我需要填充用户名和密码输入框,然后单击登录按钮。

$url = "res://ieframe.dll/invalidcert.htm?SSLError=50331648#https://10.2.2.1:8050/showme.do"
$ie = New-Object -comobject InternetExplorer.Application 
$ie.visible = $true 
$ie.silent = $true 
$ie.Navigate( $url )
while( $ie.busy){Start-Sleep 1} 
$secLink = $ie.Document.getElementsByTagName('A') | Where-Object {$_.innerText -   eq 'Continue to this website (not recommended).'} 
$secLink.click() 
$ie.Document.getElementsByType("input") | where { $.Name -eq "j_username" }.value = "user"
$ie.Document.getElementsByName("input") | where { $.Name -eq "j_password" }.value = "password"
$loginBtn = $ie.Document.getElementsById('input') | Where-Object {$_.Type -eq 'button' -and $_.Value -eq 'LoginButton'} 
$loginBtn.click() 

So right now the page opens but the input fields are not populated or the button clicked, Do I need some kind of loop or while statement?

所以现在页面打开但输入字段没有填充或按钮被点击,我需要某种循环或while语句吗?

thanks

谢谢

回答by Crystalizer

You need to wait until the page finishes loading when you've clicked on a link.

当您单击链接时,您需要等到页面完成加载。

$url = "res://ieframe.dll/invalidcert.htm?SSLError=50331648#https://10.2.2.1:8050/showme.do"
$ie = New-Object -comobject InternetExplorer.Application 
$ie.visible = $true 
$ie.silent = $true 
$ie.Navigate( $url )
while( $ie.busy){Start-Sleep 1} 
$secLink = $ie.Document.getElementsByTagName('A') | Where-Object {$_.innerText -   eq 'Continue to this website (not recommended).'} 
$secLink.click() 
while( $ie.busy){Start-Sleep 1}
$ie.Document.getElementsByType("input") | where { $.Name -eq "j_username" }.value = "user"
$ie.Document.getElementsByName("input") | where { $.Name -eq "j_password" }.value = "password"
$loginBtn = $ie.Document.getElementsById('input') | Where-Object {$_.Type -eq 'button' -and $_.Value -eq 'LoginButton'} 
$loginBtn.click() 

回答by Magnhild Hagen

Write-Output "- Kill all IE windows"
get-process iexplore | stop-process -Force
Start-Sleep -s 1

function that open IE for a given url and a password retrieved from encrypted file

为给定的 url 和从加密文件中检索的密码打开 IE 的函数

function OpenIE([string]$url, [string]$p)
{

Open Internet Explorer with a given url

使用给定的 url 打开 Internet Explorer

$wshell = New-Object -com WScript.Shell
$wshell.Run("iexplore.exe $url")
Start-Sleep 1

Send user credentials to IE

将用户凭据发送到 IE

$wshell.sendkeys("+{TAB}")
$wshell.sendkeys("username")
$wshell.sendkeys("{TAB}")
$wshell.sendkeys($p)
$wshell.sendkeys("{ENTER}")
}

$credentialsfile = "C:\temp\credfile.txt" 

Check if credential file exists

检查凭证文件是否存在

if (Test-Path $credentialsfile)
{  

Retrieve the credentials from an encrypted file

从加密文件中检索凭据

$encp = get-content $credentialsfile | convertto-securestring 
$ptr = [System.Runtime.InteropServices.Marshal]::SecureStringToCoTaskMemUnicode($encp) 
$p = [System.Runtime.InteropServices.Marshal]::PtrToStringUni($ptr) 
remove-variable encp,ptr 
}else{

Request credentials and save to encrypted file

请求凭据并保存到加密文件

$creds = Get-Credential –credential DOMAIN\user
$encp = $creds.password 
$encp |ConvertFrom-SecureString |Set-Content $credentialsfile
}

Write-Output "- open IE"
OpenIE "http://www.yoururlhere.com" $p

回答by x0n

You would probably find it a lot easier to work with something like the (free) Windows Automation module for PowerShell up on codeplex:

您可能会发现在 codeplex 上使用诸如用于 PowerShell 的(免费)Windows 自动化模块之类的东西要容易得多:

WASP is a PowerShell snapin for Windows Automation tasks like selecting windows and controls and sending mouse and keyboard events. We have automation cmdlets like Select-Window, Select-Control, Send-Keys, Send-Click, Get-WindowPosition, Set-WindowPosition, Set-WindowActive, Remove-Window ... etc.

Our goal is to enable you to accomplish most Windows GUI Automation scripting from inside PowerShell, without resorting to specialized (and expensive) scripting tools.

WASP 是用于 Windows 自动化任务的 PowerShell 管理单元,例如选择窗口和控件以及发送鼠标和键盘事件。我们有自动化 cmdlet,如 Select-Window、Select-Control、Send-Keys、Send-Click、Get-WindowPosition、Set-WindowPosition、Set-WindowActive、Remove-Window 等。

我们的目标是使您能够从 PowerShell 内部完成大多数 Windows GUI 自动化脚本,而无需求助于专门(且昂贵)的脚本工具。

http://wasp.codeplex.com/

http://wasp.codeplex.com/