【VBS】
Set ie = CreateObject("internetexplorer.application")
ie.Visible = true
'打开网页::3aTesting的BBS
ie.Navigate "www.3atesting.com/bbs"
'等待网页打开结束
delay(ie)
'获取QTP板块的链接,然后点击
Set linkAll=ie.document.getElementsByTagName("a")
Set searchLink=GetLink(linkAll,"QTP")
searchLink.click
delay(ie)
'随机取板块的某一个链接,并且点击
Set webTable=ie.Document.getElementsByTagName("Table")
Set webtableRows=webtable.item(1).getElementsByTagName("TR")
i=genRand(webtableRows.length)
Set tablelink=webtableRows.item(i).getElementsByTagName("a")
tablelink.item(1).click
delay(ie)
'获取帖子中iframe中的某一个对象,重新打开一个链接,并获取link对象,然后点击
Set iframes=ie.Document.getElementsByTagName("iframe")
i=genRand(iframes.length)
ie.Navigate iframes.item(i).src
delay(ie)
Set googlelink=ie.Document.getElementsByTagName("a")
googlelink.item(0).click
'MsgBox tablelink.length
'ie.Document.parentwindow.close
ie.Quit
Set googlelink=Nothing
Set iframes=Nothing
Set webtableRows=Nothing
Set webTable=Nothing
Set searchLink=Nothing
Set linkAll=Nothing
Set ie=Nothing
Function GetLink(objects,name)
For Each obj In objects
tmp=obj.innertext
'MsgBox tmp
If Len(tmp)>0 Then
If RegExpTest(obj.innertext,name) Then
Set GetLink=obj
Exit For
End If
End If
Next
End Function
Function RegExpTest(patrn, strng)
Dim regEx, retVal ' Create variable.
Set regEx = New RegExp ' Create regular expression.
regEx.Pattern = patrn ' Set pattern.
regEx.IgnoreCase = False ' Set case sensitivity.
retVal = regEx.Test(strng) ' Execute the search test.
RegExpTest=retVal
End Function
Sub delay(obj)
Do
WScript.Sleep 2000
Loop Until CInt(obj.readyState)=4
' MsgBox CInt(obj.readyState)
End Sub
Function genRand(count)
genRand=Int(Rnd() * count)
End Function
【watir】
require 'watir'
include Watir
ie=IE.new
ie.goto("http://www.3atesting.com/bbs")
ie.link(:text,"QTP").click
table1=Table.new(ie,:id,"forum_8")
rocount=table1.row_count()
srand(1)
i=rand(rocount)
test1=table1.column_values(3)
linkstr,tag=test1.chomp.split(//s*/ /s*/)
ie.link(:text,linkstr).click
ifra=ie.document.getElementsByTagName("IFRAME")
puts ifra.item(1).name.to_s
ie1=IE.start_process(ifra.item(1).src.to_s)
b=ie1.document.getElementsByTagName("a")
b.item(0).click
IE.close_all
在编写帖子脚本的时候遇到了个问题,这个帖子是用html编辑器的并且嵌在一个iframe中的。通过DOM查看器可以获取到到frame对象,但是编辑器就是一个html,没有什么id,name,怎么才能输入内容呢?
翻了一些关于DOM的资料,决定先把焦点定位到body中。但是无法用watir自带的方法输入内容,就采用了模拟键盘的方式解决了在编辑器编写内容的问题。@ie.frame(:id, “bulletin_editor”).document.body.focus()
@ie.sendkeys(”aaaaaaaaaaaaa”)
不过,这里遇到了个问题,当输入的内容是中文时,输入的是乱码,还不知道该怎么解决呢?