51Testing软件测试论坛

标题: 如何写一个函数关闭指定的IE窗口? [打印本页]

作者: gjay1688    时间: 2010-2-24 11:06
标题: 如何写一个函数关闭指定的IE窗口?
如何写一个函数关闭指定的IE窗口?
作者: zzxxbb112    时间: 2010-2-24 11:07
Browser().close
作者: 风雪夜归人    时间: 2010-2-24 12:34
Sub CloseIE(strIETitle)
        Set shell = CreateObject("Shell.Application")
        Set shellWindows = shell.Windows
        For Each ie In shellWindows
                If   InStr(1,  ie.FullName,   "IEXPLORE.EXE",   vbTextCompare)   <>   0 then
                        If Instr(1, Window("hwnd:=" &ie.HWND).GetROProperty("text"), strIETitle, vbTextCompare) <> 0 Then
                                SystemUtil.CloseProcessByHwnd ie.HWND
                        End If
                End if
        Next
End Sub


CloseIE "看到的IEtitle“
作者: gjay1688    时间: 2010-2-24 13:40
多谢,不过问一下
If   InStr(1,  ie.FullName,   "IEXPLORE.EXE",   vbTextCompare)   <>   0 then
这句为什么要判断呢?
作者: 风雪夜归人    时间: 2010-2-24 14:04
判断是否为IE,你可以这样试一下效果

Sub CloseIE(strIETitle)
        Set shell = CreateObject("Shell.Application")
        Set shellWindows = shell.Windows
        For Each ie In shellWindows
             Msgbox ie.fullname
        Next
End Sub
作者: gjay1688    时间: 2010-2-24 14:13
可是既然是ie.FullName,不就肯定是IEXPLORE.EXE吗?不需要判断吧。
作者: 风雪夜归人    时间: 2010-2-24 14:44
For Each ie In shellWindows 注意这句话,ie仅仅是我定义的一个在shellWindows里面的一个实体,而不是传说中的internetexplorer,或者说如果我这样定义 For Each aaaa In shellWindows 则用: aaa. FullName
作者: gjay1688    时间: 2010-2-24 15:16
知道了,多谢指教。
作者: gjay1688    时间: 2010-2-24 15:34
但是为什么我用下面的代码,显示的都是IE而没有其他程序呢?
        Set shell = CreateObject("Shell.Application")
        Set shellWindows = shell.Windows
        For Each aaa In shellWindows
             Msgbox aaa.fullname
        Next
作者: 风雪夜归人    时间: 2010-2-24 16:08
会有的,只是没有特定的环境而已~
作者: wangsc_testing    时间: 2010-2-25 21:04
InStr 函数用于返回某字符串在另一字符串中第一次出现的位置。
格式如下:
InStr([start, ]string1, string2[, compare])

参数说明
start:可选项。数值表达式,用于设置每次搜索的开始位置。如果省略,将从第一个字符的位置开始搜索。如果 start 包含 Null,则会出现错误。如果已指定 compare,则必须要有 start 参数。

string1:必选项。接受搜索的字符串表达式。

string2:必选项。要搜索的字符串表达式。

compare:可选项。指示在计算子字符串时使用的比较类型的数值。有关数值,请参阅“设置”部分。如果省略,将执行二进制比较。

设置方法:
compare 参数可以有以下值:
常数                  值                        描述
vbBinaryCompare            0                                          执行二进制比较。
vbTextCompare                1                                              执行文本比较。


返回值
InStr 函数返回以下值:

如果                           InStr 返回
string1 为零长度                    0
string1 为 Null                                        Null
string2 为零长度                   start
string2 为 Null                                       Null
string2 没有找到                    0
在string1 中找到 string2          找到匹配字符串的位置
start > Len(string2)                                  0


实例说明
下面的示例利用 InStr 搜索字符串:

Dim SearchString, SearchChar, MyPos
SearchString ="XXpXXpXXPXXP"   ' String to search in.
SearchChar = "P"   ' Search for "P".
MyPos = Instr(4, SearchString, SearchChar, 1)   ' A textual comparison starting at position 4. Returns 6.
MyPos = Instr(1, SearchString, SearchChar, 0)   ' A binary comparison starting at position 1. Returns 9.   
MyPos = Instr(SearchString, SearchChar)   ' Comparison is binary by default (last argument is omitted). Returns 9.
MyPos = Instr(1, SearchString, "W")   ' A binary comparison starting at position 1. Returns 0 ("W" is not found).
注意 InStrB 函数使用包含在字符串中的字节数据,所以 InStrB 返回的不是一个字符串在另一个字符串中第一次出现的字符位置,而是字节位置。
作者: JonnyGu    时间: 2010-3-15 10:18
把GetROProperty("text")改成GetROProperty("title"),打开51的首页以及51论坛两个Browser,调用CloseIE("51Testing软件测试网-中国软件测试人的精神家园"),结果把两个browser都关掉了啊,这是怎么回事?
作者: lamuda    时间: 2010-3-15 12:02
SystemUtil.CloseProcessByHwnd ie.HWND
这句经常会把整个IE关掉,不知道是怎么回事




欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2