【问题标题】:How to properly access elements of Internet Explorer?如何正确访问 Internet Explorer 的元素?
【发布时间】:2019-07-07 15:57:27
【问题描述】:

我有一个脚本可以测试我们的一个网站是否正常运行。

  1. 测试 1:它检查 HTTP 状态。 (这里没问题)
  2. 测试 2:它检查用户授权是否有效,因为我们过去曾遇到过此问题。

然后它等待一小时并再次运行两个测试。

脚本整体结构如下(伪代码):

while (1 -eq 1) {
    test1
    if ($result -eq "pass") {
        test2
    }
    start-sleep -seconds 3600
}

测试 2 是出现问题的地方。该测试的工作原理是:导航到登录页面,输入凭据,单击登录按钮。检查 URL,因为成功登录会导致与登录页面不同的 URL。这工作正常,除了(看似随机的)循环它无法访问页面的元素:用户名和密码字段以及登录按钮。抛出的异常取决于我如何更改代码以尝试解决问题。

我收到的一些错误消息包括:

System.Runtime.InteropServices.COMException RPC 服务器是 不可用。 (HRESULT 异常:0x800706BA)

System.Runtime.InteropServices.COMException 在此对象上找不到属性“值”。验证该属性是否存在并且 可以设置。

System.Runtime.InteropServices.COMException OperationStopped: (:) [], COMException H结果:-2147352319 不支持这样的接口

System.Runtime.InteropServices.COMException OperationStopped: (:) [], COMException H结果:-2147352319 不支持此命令。

违规代码是任何尝试使用getElementByID 访问页面元素的代码,例如:

$ie.Document.getElementByID("username").value = $userame

再次,奇怪的是代码运行良好的几个小时......但是,通常在半夜,它随机变得无法访问页面元素。

以下是测试 2 的完整代码,以防万一:

$ie = New-Object -ComObject "internetExplorer.Application"
#$ie.Visible = $true
$ie.navigate2($loginPage)
while ($ie.busy -eq $true) {start-sleep -seconds 1}
$ie.Document.getElementByID("username").value = $username
$ie.Document.getElementByID("password").value = $password
$ie.Document.getElementByID("login-button").click()
while ($ie.busy -eq $true) {start-sleep -seconds 1}
if ($ie.Document.url -eq $landingPage) {
    #success
} else {
    #failure
}
$ie.quit()
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($ie) | out-null
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
$process = get-process -name "iexplore"
while ($process) {
    try {
        stop-process -name "iexplore" -ea stop
    } catch [Microsoft.PowerShell.Commands.ProcessCommandException] {
        #do nothing
    }
    try {
        $process = get-process -name "iexplore" -ea stop
    } catch [Microsoft.PowerShell.Commands.ProcessCommandException] {
        $process = $null
    }
}

【问题讨论】:

    标签: powershell dom com internet-explorer-11


    【解决方案1】:

    在找出问题所在之前,我已经实施了一种解决方法。我将违规代码放在try 块中。在catch 块中,我重新启动脚本。以下不是它在我的脚本中的实现方式,但它为您提供了思路:

    try {
        $ie.Document.getElementByID("username").value = $username
    } catch {
        start powershell {$scriptPath}
        exit
    }
    

    当脚本重新启动时,可以再次使用getElementByID 访问页面元素。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-08
      • 1970-01-01
      • 2018-08-01
      • 2019-06-17
      • 2015-05-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多