【问题标题】:AppActivate doesn't always activate the application windowAppActivate 并不总是激活应用程序窗口
【发布时间】:2016-08-03 16:41:19
【问题描述】:

因此,我正在开发一些可以使 Internet Explorer 自动化的 VBScript:打开、导航和下载文件。我已经让它工作了,但是有一个问题在于我需要向它发送击键。 SendKeys 对我有用,但问题是当我使用 AppActivate 将焦点设置在 IE 上时,它并不总是有效。基本上可以分为这两种情况:

案例1:双击我的.vbs文件,自动化过程顺利进行。窗口已激活,一切正常。

案例 2:双击我的 .vbs 文件,其他一些应用程序打开一个窃取焦点的窗口(或手动单击其他内容以更改焦点)。稍后执行AppActivate 行时,IE 永远不会获得焦点,并且击键显然不会通过。

那么你们对为什么AppActivate 没有设置焦点有什么想法吗?

这是我用来激活 IE 窗口的代码:

Do While Not wshell.AppActivate("Active Agents - Internet Explorer")
    WScript.Sleep(1)
Loop

【问题讨论】:

    标签: internet-explorer vbscript automation focus


    【解决方案1】:

    此脚本示例运行 IE 并使用 AppActivate 焦点发送密钥

        'fuction to set the application active and then send keys 
        Function mySendKey(sKey,sTime)
           for i=0 to 600        ' this loop will continue about 30 sec if this not enough increase this number
            'get the window title of the IE
            title= sh.Exec("cmd.exe /c tasklist /v /fi ""imagename EQ iexplore*"" /FO LIST | FIND /i ""window title:"" ").stdOut.readline
            title= Mid (title,15,len(title)-14)
            sh.Popup  title,1
    
            Sh.AppActivate title
            Rtn=Sh.AppActivate(title)     '  window title of application
            If Rtn = True  Then           'if the window active then execute sned keys
                Sh.SendKeys  sKey                                     
                Exit For 
            End If
            wscript.sleep 10                              
          Next
         WScript.Sleep sTime*1000
        End Function
    
      'hide the cmd window from popup 
      If StrComp(right(WScript.FullName,11),"wscript.exe",1) = 0 Then    
      WScript.Quit CreateObject("WScript.Shell").Run("cscript.exe //nologo """ & WScript.ScriptFullName & """", 0, False)
      End If 
      'open internet explorer   
    Set Sh = CreateObject("WScript.Shell")
    Sh.Exec "C:\Program Files\Internet Explorer\iexplore.exe"
    WScript.Sleep 7000
     'call mySendKey function to send keys to IE  
     Call mySendKey("(% )(x)",1)                         'max the windows of IE
     Call mySendKey("({F6})(www.yahoo.com)    (~)",10)   'browse to yahoo and wait 10 sec to load
     Call mySendKey("({F6})(www.msn.com)      (~)",10)   'browse to msn and wait 10 sec to load 
     Call mySendKey("(% )(C)",1)                         'close the IE
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多