【问题标题】:internetexplorer.application document.getElementByID returns as null / Powershellinternetexplorer.application document.getElementByID 返回 null / Powershell
【发布时间】:2015-02-02 15:16:00
【问题描述】:

正在努力解决以下问题。

查看 HTML 的摘录,ID 存在,但它一直返回空表达式。不幸的是,我不能在网站上提供更多细节......我做错了什么明显吗?

--- HTML 的提取 ----

<input name="ctl00$mainCopy$LoginView$LoginControl$UserName" type="text" id="ctl00_mainCopy_LoginView_LoginControl_UserName" tabindex="1" class="txtBox" onchange="document.getElementById('ctl00_mainCopy_LoginView_LoginControl_PasswordResetLink').disabled
= false;" style="width:150px;" />

--- 脚本---

$ie = New-Object -com internetexplorer.application
$ie.visible = $true
$ie.navigate("excluded for security")

$ie.document.getElementByID("ctl00_mainCopy_LoginView_LoginControl_UserName").Value= "excluded for security"

---- 错误----

You cannot call a method on a null-valued expression.
At line:1 char:1
+ $ie.document.getElementByID("ctl00_mainCopy_LoginView_LoginControl_UserName").Va ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

【问题讨论】:

    标签: internet-explorer powershell web-scraping


    【解决方案1】:

    here 也解决了这个问题。

    我将假设您的 URL 位于安全区域,例如“本地 Intranet”或“受信任的站点”。要对此进行测试,请尝试使用您的代码导航到 www.google.com,而不是“出于安全考虑”。

    IE 对象使用 Internet 区域上下文打开。有区域提升安全机制来防止脚本(而不是用户)从一个安全区域导航到另一个。 Zone Elevation Restrictions

    根据您的需求以及您是否有其他限制,此问题有多种解决方案。这种解决方法是以管理员身份启动 PowerShell,然后以管理员身份打开 IE。 Other solutions by MS.

    希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      尝试在$ie.navigate 命令之后设置睡眠,例如:

      while ($ie.Busy() -and $ie.ReadyState -ne 4){ sleep -Milliseconds 100 }
      

      当您尝试获取元素时,您的页面似乎仍在加载。

      【讨论】:

      • 对我来说,$ie.Busy 仅用作属性,而不是像代码 sn-p 中的方法调用。
      • 你是对的,自从我写了那个答案之后,它可能已经改变了
      猜你喜欢
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 2011-11-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-02
      • 2021-03-05
      相关资源
      最近更新 更多