Powershell - Internet Explorer - 获取新打开的选项卡的文档

时间:2013-12-28 10:31:24

标签: windows internet-explorer powershell automation

我试图弄清楚如何使用Windows 7 x64上的PowerShell版本2登录Internet Explorer 9中的网页。当我在PowerShell中逐一运行这些行时,我得到了我想要的 - 页面打开,我可以登录。

$internet_explorer = new-object -com "InternetExplorer.Application"
$internet_explorer.visible = $true
$internet_explorer.navigate2("https://abcd.efgh.com/ijkl/Login.jsp")
$this_document = $internet_explorer.document
$username_field = $this_document.getElementByID("usernameField")
$password_field = $this_document.getElementByID("passwordField")
$login_button = $this_document.getElementByID("SubmitButton")
$username_field.value = "username"
$password_field.value = "password"
$login_button.click()

但是当我尝试打开另一个标签并尝试通过逐行执行以下行来登录新网页时,

$internet_explorer.navigate2("https://mnop.qrst.com/uvwx/Login.jsp", 0x0800)
$this_document = $internet_explorer.document
$username_field = $this_document.getElementByID("usernameField")
$password_field = $this_document.getElementByID("passwordField")
$login_button = $this_document.getElementByID("SubmitButton")
$username_field.value = "username"
$password_field.value = "password"
$login_button.click()

我收到以下错误:

Property 'value' cannot be found on this object; make sure it exists and is settable.
At line:1 char:17
+ $username_field. <<<< value = "username"
    + CategoryInfo          : InvalidOperation: (value:String) [], RuntimeException
    + FullyQualifiedErrorId : PropertyNotFound

Property 'value' cannot be found on this object; make sure it exists and is settable.
At line:1 char:17
+ $password_field. <<<< value = "password"
    + CategoryInfo          : InvalidOperation: (value:String) [], RuntimeException
    + FullyQualifiedErrorId : PropertyNotFound

You cannot call a method on a null-valued expression.
At line:1 char:20
+ $login_button.click <<<< ()
    + CategoryInfo          : InvalidOperation: (click:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

因此,当我打开一个新标签时,如何获得新打开标签的Document?请帮我解决这个问题

编辑:替代方法会做。如果它们存在,请随时建议以任何其他方式自动登录多个网页(如上所述)

1 个答案:

答案 0 :(得分:0)

documentation for the Navigate method开始,新选项卡似乎不可用于自动化:

  

当指定了navOpenInNewWindow或navOpenInNewTab时,调用者没有收到指向新窗口的 IWebBrowser2 界面的指针,因此没有直接的方法来操纵它。