使用VB脚本时IE失去焦点

时间:2018-11-16 21:25:59

标签: testing vbscript automation

我正在尝试启动IE,并将calc.exe路径作为URL传递并下载。但是在传递URL之后,WshShell的焦点移到了另一点。下面是我的脚本。

strWebSite = "file://C:/Windows/System32/calc.exe"
Set ie = GetObject ("", "internetexplorer.application")
ie.Navigate2 strWebSite
WScript.Sleep 500
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.AppActivate("Internet Explorer")
WScript.Sleep 3000
WshShell.SendKeys "{TAB}"
WshShell.SendKeys "{ENTER}"
WScript.Sleep 3000
WshShell.SendKeys "{TAB}"
WScript.Sleep 3000
WshShell.SendKeys "{TAB}"
WshShell.SendKeys "{ENTER}"

1 个答案:

答案 0 :(得分:0)

已解决:通过进程ID激活IE

strWebSite = "file://C:/Windows/System32/calc.exe"
Set ie = GetObject ("", "internetexplorer.application")
ie.Navigate2 strWebSite
WScript.Sleep 500
Set WshShell = WScript.CreateObject("WScript.Shell")
Set Processes = GetObject("winmgmts:").InstancesOf("Win32_Process")

intProcessId = ""
For Each Process In Processes
  If StrComp(Process.Name, "iexplore.exe", vbTextCompare) = 0 Then
      intProcessId = Process.ProcessId
      Exit For
   End If
Next
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.AppActivate intProcessId 
WScript.Sleep 3000
WshShell.SendKeys "{TAB}"
WshShell.SendKeys "{ENTER}"
WScript.Sleep 3000
WshShell.SendKeys "{TAB}"
WScript.Sleep 3000
WshShell.SendKeys "{TAB}"
WshShell.SendKeys "{ENTER}"