带有Powershell的IE自动化 [英] IE Automation with Powershell

查看:172
本文介绍了带有Powershell的IE自动化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Powershell和IE自动登录到我们Intranet上的网站.到目前为止,我有以下有效的代码:

I am attempting to automate the login to website on our intranet using Powershell and IE. So far, I have the following code that works:

$ie = new-object -com "InternetExplorer.Application" 
$ie.navigate("https://somepage/jsp/module/Login.jsp/")
while($ie.ReadyState -ne 4) {start-sleep 1} 
$ie.visible = $true 
$doc = $ie.Document
If ($doc.nameProp -eq "Certificate Error: Navigation Blocked") {
    $doc.getElementByID("overridelink").Click()
}
$loginid = $doc.getElementById("loginid") 
$loginid.value= "username"
$password = $doc.getElementById("password")
$password.value = 'somepass'
$ie.navigate("javascript:doSubmit('login')",$null,$true)

因此,我现在遇到的问题是该网站关闭了用于登录的原始窗口,并打开了一个新的IE窗口.如何将输入提交到新窗口?我知道我可以使用tasklist.exe/v之类的东西来获取新窗口的PID ...但是我不确定如何控制它.

So, the problem area I have now is that the site closes the original window used to login and opens a new IE window. How do I go about submitting inputs to that new window? I know I can use something like tasklist.exe /v to get the PID of the new window... but I'm not sure how I would go about taking control of it.

另外,在查看我的代码后,请知道我不打算使用嵌入式用户名和密码.这样做只是为了避免我每次要测试脚本时都必须连续输入un/pw组合键的情况.

Also, after viewing my code, please know I do not intend to use embedded user name and passwords. That's only in place so that I don't have to continually type a un/pw combo every time I want to test the script.

推荐答案

我发现了一个旧的

I found an old article that describes how to do what you're looking for, by looping through the windows of a Shell.Application object. I have to say that while it looks possible and does seem to answer your direct question, the approach seems pretty unpleasant and fragile to me.

如果您不反对尝试其他方法,建议您使用 Selenium Webdriver .您可以使用Internet Explorer驱动程序以及 文档中的C#示例 通常可以很好地转换为PowerShell.您还可以获得其他一些不错的好处,例如其他Web浏览器的驱动程序或等待条件而不是依赖睡眠/检查循环的功能.您还将具有driver.switchTo()功能,可让您在窗口或框架之间跳动.

If you're not averse to trying a different approach, I would suggest giving Selenium Webdriver a shot. You can use the Internet Explorer driver, and the C# examples in the documentation generally translate nicely into PowerShell. You get some other nice benefits too, like drivers for other web browsers or the ability to wait for a condition instead of relying on sleep/check loops. You will also have a driver.switchTo() function that allows you to bounce between windows or frames.

这篇关于带有Powershell的IE自动化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆