IE HTML DOM的应用

'等待页面加载:

'使用IE COM启动IE

Set oIE = CreateObject("InternetExplorer.Application")

oIE.Visible = True '设置可见

oIE.Navigate "http://www.baidu.com" '跳转URL

'等待IE页面加载完毕

While oIE.Busy: Wend

 

'利用DOM操作测试对象

'(1)通过getElementByID方法获取定位对象,并对其进行操作:

'使用IE COM启动IE

oIE.Visible = True '设置可见

oIE.Navigate "http://www.baidu.com" '跳转URL

'等待IE页面加载完毕

While oIE.Busy: Wend

'获取Document对象

Set oDoc = oIE.Document

'使用DOM对测试对象进行操作

With oDoc

'搜索框输入

.getElementByID("kw").value = "zzxxbb112"

'点击百度搜索

.getElementByID("sb").Click

End With

Set oDoc = Nothing

Set oIE = Nothing

'(2)通过getElementByName方法获取定位对象,并对其进行操作:

'使用IE COM启动IE

oIE.Visible = True '设置可见

oIE.Navigate "http://www.baidu.com" '跳转URL

'等待IE页面加载完毕

While oIE.Busy: Wend

'获取Document对象

Set oDoc = oIE.Document

'获取元素名为WD的集合

Set oEdits = oDoc.getElementsByName("wd")

'遍历对象并对其进行操作

For Each oEdit In oEdits

 oEdit.value = "zzxxbb112"

Next

'点击百度搜索

oDoc.getElementByID("sb").Click

Set oDoc = Nothing

Set oIE = Nothing

'(3)通过getElementsByTagName方法获取定位对象,并对其进行操作:

'使用IE COM启动IE

oIE.Visible = True '设置可见

oIE.Navigate "http://www.baidu.com" '跳转URL

'等待IE页面加载完毕

While oIE.Busy: Wend

'获取Document对象

Set oDoc = oIE.Document

'获取TAG名为INPUT的元素集合

Set oEdits = oDoc.getElementsByTagName("INPUT")

'遍历对象并判断文本框对其进行操作

For Each oEdit In oEdits

 If oEdit.type = "text" then

  oEdit.value = "zzxxbb112"

 End if

Next

'点击百度搜索

oDoc.getElementsByID("sb").Click

Set oDoc = Nothing

Set oIE = Nothing

 

posted @ 2012-09-07 10:36  dushuai  阅读(278)  评论(0编辑  收藏  举报