Powershell IE登录自动化无法按ID查找元素

时间:2017-10-15 13:14:34

标签: powershell internet-explorer login automation getelementbyid

知道PowerShell的人是否可以告诉我为什么我无法使用其ID“user_name”找到用户名输入字段?我已经在其他网站上尝试了这个并且已经有效但是对于这个它只是拒绝找到它。我还尝试了IHTMLDocument3_getElementsByName方法以及IHTMLDocument3_getElementsByTagName来查找“输入”甚至“div”之类的内容。一些div类弹出,但有很多在那里丢失,我不知道它为什么会丢失。请告诉我光明。

#ID names
$IDuserName = "user_name"
$IDuserPass = "user_password"

$url = "https://isssupport.service-now.com/"
$ie = New-Object -com InternetExplorer.Application
$ie.visible = $true
$ie.navigate($URL)

While ($IE.Busy -eq $true) {Start-Sleep -Milliseconds 1000} 

$ie.Document.IHTMLDocument3_getElementByID($IDuserName).Value = $userName

以下是用户名输入字段的HTML行

<input name="user_name" id="user_name" type="text" class="form-control" value="" autocomplete="off">

1 个答案:

答案 0 :(得分:0)

该页面使用大量的javascript构建。如果您尝试在浏览器中未启用JavaScript的情况下打开页面,您甚至无法看到登录表单。这似乎是IE / PowerShell找到表单及其元素的问题。

但是,您可以将Internet Explorer窗口置于前台,并使用Systems.Windows.Forms与页面进行交互,如下所示:

#ID names
$IDuserName = "user_name"
$IDuserPass = "user_password"

$url = "https://isssupport.service-now.com/"
$ie = New-Object -com InternetExplorer.Application
$ie.visible = $true

$wshell = New-Object -ComObject wscript.shell;
$wshell.AppActivate($ie.Name)
Start-Sleep -Milliseconds 500

$ie.navigate($url)

While ($ie.Busy -eq $true) {Start-Sleep -Milliseconds 1000}

Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.SendKeys]::SendWait('username');
[System.Windows.Forms.SendKeys]::SendWait('{TAB}');
[System.Windows.Forms.SendKeys]::SendWait('password');
[System.Windows.Forms.SendKeys]::SendWait('{ENTER}');

请问您正在构建的脚本的目的是什么?

祝你好运

京特