【问题标题】:Powershell - Internet Explorer - Get document for a newly opened tabPowershell - Internet Explorer - 获取新打开选项卡的文档
【发布时间】:2014-01-15 19:14:59
【问题描述】:

我试图弄清楚如何在 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?请帮我解决这个问题

编辑:替代方法可以。如果它们存在,请随意建议任何其他方式来自动登录多个网页(如上面尝试的方式)

【问题讨论】:

    标签: windows internet-explorer powershell automation


    【解决方案1】:

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

    当指定了 navOpenInNewWindow 或 navOpenInNewTab 时,调用者不会收到指向新窗口的 IWebBrowser2 接口的指针,因此无法立即对其进行操作。

    【讨论】:

    • 那么有没有其他方法可以实现登录多个网页的初衷(例如使用多个窗口、使用其他方法、使用其他浏览器、使用其他工具等)?跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-08
    • 1970-01-01
    • 2013-04-16
    • 2020-09-27
    • 1970-01-01
    相关资源
    最近更新 更多