如何使用VBScript在打开的IE网页上进行自动化

时间:2015-12-28 12:56:22

标签: internet-explorer vbscript automation webpage

我想点击按钮,然后使用vb脚本在打开的网页(IE)上选择下拉菜单。

我尝试了一些,但有些时候没有工作。

代码段

Option Explicit
Dim IE,WshShell,surl,shapp
surl="https://oss.corp.hp.com/os/os.tcl?modifyrep=AA+OF+Express+shipment"
set shapp=createobject("shell.application")
set WshShell = WScript.CreateObject("WScript.Shell") 
While IE.Busy
  WScript.Sleep 1000
Wend
For Each IE In shapp.Windows
    For Each a In ie.document.getElementsByTagname("a")
        If a.href = "javascript:DelFilters()" Then
            a.Click
        End If  
    Next
    Wscript.Sleep 10000
    IE.Document.All.Item("filterfields").SelectedIndex = 219
    IE.Document.All.Item("filterfields").FireEvent ("onchange")
    'Wscript.Sleep 10000
    Set Helem = IE.document.getElementByID("filterval")
    Helem.Value = "82P980080009"


Next
    set shapp=Nothing

1 个答案:

答案 0 :(得分:0)

不需要 WScript.Shell 。使用 InternetExplorer.Application 。它将为您打开IE:

Option Explicit
Dim IE,surl
surl="https://oss.corp.hp.com/os/os.tcl?modifyrep=AA+OF+Express+shipment"

'IE init:
Set IE = WScript.CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate surl

While IE.Busy
  WScript.Sleep 1000
Wend

'from now on, it depends on the page contents:
For Each a In IE.document.getElementsByTagname("a")
    If a.href = "javascript:DelFilters()" Then
        a.Click
    End If  
Next
Wscript.Sleep 10000
IE.Document.All.Item("filterfields").SelectedIndex = 219
IE.Document.All.Item("filterfields").FireEvent ("onchange")
'Wscript.Sleep 10000
Set Helem = IE.document.getElementByID("filterval")
Helem.Value = "82P980080009"

我无法访问您的示例页面,因此我尝试了其他人。不确定在IE打开并且调用IE.document的方法后是否会出现错误,但我认为你的问题是开始使用IE浏览器。