【问题标题】:when i am running my script in compatibility mode i am getting following error?当我在兼容模式下运行脚本时出现以下错误?
【发布时间】:2014-05-23 23:16:29
【问题描述】:

我想自动化自动 Web 登录,此代码与 ie9 和某些表单(使用表单标签开发)很好。但是当我为使用标签表构建的表单运行相同的脚本时,它是在 Ie10、windows8 中不使用 id,但在 IE9 windows7 中使用相同的脚本使用 id,但在我的工作环境中,现在我们主要使用的是 windows8 机器。所以我开始知道如果我们在兼容模式下运行,当我添加代码以在兼容模式下运行时,我们可以解决问题,我收到了这个错误。我不明白该怎么办..

#include <IE.au3>
    #include <GuiButton.au3>
    #include <File.au3>
    #RequireAdmin

        If IsAdmin() then
        $64Bit = ""
            If @OSArch = "X64" Then
                $64Bit = "64"
            EndIf
        If StringLeft(RegRead("HKLM" & $64Bit & "\SOFTWARE\Microsoft\Internet Explorer\Version Vector", "IE"), 1) > 8 Then ;Check for version 9 or later


          $wshNetwork = ObjCreate("WScript.Network")
          $struser = $wshNetwork.Username
          $objWMIService = ObjGet("winmgmts:\\.\root\cimv2")
          $objAcc = $objWMIService.Get('Win32_UserAccount.Name="' & $struser & '",Domain="' & @ComputerName & '"')
          $objAccount = $objAcc.SID
          RegWrite("HKU" & $64Bit & "\" & $objAccount & "\Software\Microsoft\Internet Explorer\BrowserEmulation\", "AllSitesCompatibilityMode", "REG_DWORD", 1)
          RegWrite("HKU\" & $objAccount & "\Software\Microsoft\Internet Explorer\BrowserEmulation\", "AllSitesCompatibilityMode", "REG_DWORD", 1)
        EndIf
        EndIf

    $url = "https://190.198.14.15/"
    $formID = ""
    $formUID = "username"
    $uName = "admin"
    $formPID = "password"
    $pwd = "SeR^ER@iL0"
    $formSubmit = "ID_LOGON"

    ;Launch the Internet Explorer as a private session
    ShellExecute ("iexplore.exe", " -private about:blank", @programFilesDir & "\Internet Explorer\iexplore.exe", "open", @SW_MAXIMIZE)
    WinWait ("Blank Page")
    $oIE = _IEAttach ("about:blank", "url")

    ;Wait for the IE to launch
    _IELoadWait ($oIE)

    ;Navigate to the given URL
    _IENavigate ($oIE, $url)

    ;Get the IE process id specific to this instance
    Local $PID = WinGetProcess(_IEPropertyGet($oIE, "hwnd"))

    ;Print the PID in the console
    If $PID Then
        ;MsgBox(0, "Example", "Internet Explorer is running.")
        ;MsgBox(0,"Process ID",$PID)
        ConsoleWrite("["&$PID&"]")
    Else
        MsgBox(0, "Example", "Unable to get the process id of IE instance")
    EndIf

    ;Disable IE address bar and menu bar
    _IEPropertySet ($oIE, "addressbar", False)
    _IEPropertySet ($oIE, "menubar", False)

    ;Click on 'Continue to this website' option if there is any HTTPS certificate Warning
    while(_IELinkClickByText ($oIE, "Continue to this website (not recommended)."))
            _IELoadWait ($oIE,10000)
    wend

    ;Get the field id and fill with provided value
    ;$oIE.document.getElementById($formUID).value = $uName
    $oIE.document.getElementsByName($formUID).Item(0).value = $uName
    $oIE.document.getElementById($formPID).value = $pwd

    ;$oSubmit = _IEGetObjByName ($oIE, $formSubmit)
    $oSubmit = $oIE.document.getElementById($formSubmit)
    _IEAction ($oSubmit, "click")

当我运行这个时,我得到一个错误:

>"C:\Program Files (x86)\AutoIt3\SciTE\..\autoit3.exe" /ErrorStdOut "D:\My_Files\Automation scripts\My tests\AutomaticWebpageLogin -1.au3"    
"D:\My_Files\Automation scripts\My tests\AutomaticWebpageLogin -1.au3" (18) : ==> Variable must be of type "Object".:
$objAccount = $objAcc.SID
$objAccount = $objAcc^ ERROR
>Exit code: 1    Time: 0.697

【问题讨论】:

  • 看看哪里出了问题,整个 IE 自动化代码肯定是无关紧要的,因为它永远不会到达它。如果您自己尝试this code,它会起作用吗?有一个对人们的计算机进行永久注册表更改的示例也不理想,您至少应该先警告人们。
  • 我的意图不是在某些论坛中更改注册表设置我读到,如果我们更改注册表,那么 IE 兼容模式将会改变。我的实际问题是上面没有注册表更改的代码在 Windows 中与 IE9 一起工作正常7 但不适用于 IE10 windows 8
  • 好的,但是查看您收到的错误消息,具体问题是使用 WMI 获取用户 SID。关于 IE 的所有内容实际上与您遇到的错误无关。调试的下一步是在从 WMI 获取变量(如 $struser)后检查它们的值,并尝试查看问题出在哪里。由于此代码在带有 IE11 的 Windows 8 中对我来说运行良好(据我所知),除了说错误消息意味着您应该将精力集中在有关 WMI 的那 5 行之外,我无法真正帮助您。

标签: internet-explorer-10 autoit


【解决方案1】:

您的身份验证过程中发生错误($objAcc = 行),可能是密码或用户名错误。

如果发生错误,WMI 服务的 .Get 函数可能不会返回对象。

检查这样的错误

if(@error <> 0) Then
  ; ERROR HANDLER
EndIf 

并检查帐户信息是否正确。 Look at your other question

参考Win32_UserAccount

【讨论】: