在已打开的浏览器 window 中单击 href link
Click on a href link in an already opened browser window
在下面的代码中,我试图点击 www.google.co.in 网站中的 "About" link (href
)。这适用于 IE11 (Windows 10),但不适用于 IE10 (Windows 7)。无论如何,这是否取决于机器。如果不是,正确的代码是什么?
请记住,我正在尝试在已打开的浏览器 window 中单击 link。
Set objShell = CreateObject("Shell.Application")
IE_count = objShell.Windows.Count
For x = 0 To (IE_count - 1)
On Error Resume Next ' sometimes more web pages are counted than are open
my_url = objShell.Windows(x).Document.Location
my_title = objShell.Windows(x).Document.Title
'You can use my_title of my_url, whichever you want
If my_title Like "Google" & "*" Then 'identify the existing web page
Set ie = objShell.Windows(x)
Exit For
Else
End If
Next
Dim LinkHref
Dim a
LinkHref = "//www.google.co.in/intl/en/about.html?fg=1"
For Each a In ie.Document.GetElementsByTagName("A")
If LCase(a.GetAttribute("href")) = LCase(LinkHref) Then
a.Click
Exit For ''# to stop after the first hit
End If
Next
不知道 QTP,但 VBScript 没有 Like
运算符。
这是在纯 VBScript 中附加到具有特定标题的 IE window 的常用方法:
Set app = CreateObject("Shell.Application")
For Each wnd In app.Windows
If wnd.Name = "Internet Explorer" Then
If InStr(1, wnd.Document.Title, "Google", vbTextCompare) > 0 Then
Set ie = wnd
Exit For
End If
End If
Next
您可以通过 QTP 中的描述性编程来实现目标(如果您出于某种原因不想使用对象存储库)。这段代码应该给你一个你可以做什么的例子:
Dim oDesc ' create a Description object for objects of class Link
Set oDesc = Description.Create
oDesc("micclass").value = "Link"
'Find all the Links in the browser using ChildObjects
Set obj = Browser("title=Google").Page("title=Google").ChildObjects(oDesc)
Dim i
'obj.Count value has the number of links in the page
For i = 0 to obj.Count - 1 ' indexed from zero, so use 0 to Count -1
'get the name of all the links in the page
If obj(i).GetROProperty("innerhtml")= LinkHref Then
obj(i).Click 'click the link if it matched the href you specfied
Exit For ' no need to carry on the loop if we found the right link
End If
Next
如果你只是需要使用vbscript,你可以这样做:
Dim oShell : Set oShell = CreateObject("Shell.Application")
Dim oWindow
For Each oWindow In oShell.Windows
If InStr(oWindow.FullName, "iexplore") > 0 Then
If InStr(1, oWindow.Document.Title, "Google", vbTextCompare) > 0 Then
Set ieApp = oWindow
Exit For
End If
End If
Next
LinkHref = "//www.google.co.in/intl/en/about.html?fg=1"
For Each linky In ieApp.Document.GetElementsbyTagName("a")
If LCase(linky.GetAttribute("href")) = LCase(LinkHref) Then
linky.Click
Exit For
End If
Next
这几乎是 Ansgar 上面给出的答案,但有一些额外的内容来修复对象错误。只有浏览器 window 具有 Document.Title
,并且循环会遍历每个打开的 window,因此当循环尝试非 IE window 时会出现错误。如果 window 首先被识别为 IE 实例,此版本通过仅检查 Document.Title
来修复此问题。
在下面的代码中,我试图点击 www.google.co.in 网站中的 "About" link (href
)。这适用于 IE11 (Windows 10),但不适用于 IE10 (Windows 7)。无论如何,这是否取决于机器。如果不是,正确的代码是什么?
请记住,我正在尝试在已打开的浏览器 window 中单击 link。
Set objShell = CreateObject("Shell.Application")
IE_count = objShell.Windows.Count
For x = 0 To (IE_count - 1)
On Error Resume Next ' sometimes more web pages are counted than are open
my_url = objShell.Windows(x).Document.Location
my_title = objShell.Windows(x).Document.Title
'You can use my_title of my_url, whichever you want
If my_title Like "Google" & "*" Then 'identify the existing web page
Set ie = objShell.Windows(x)
Exit For
Else
End If
Next
Dim LinkHref
Dim a
LinkHref = "//www.google.co.in/intl/en/about.html?fg=1"
For Each a In ie.Document.GetElementsByTagName("A")
If LCase(a.GetAttribute("href")) = LCase(LinkHref) Then
a.Click
Exit For ''# to stop after the first hit
End If
Next
不知道 QTP,但 VBScript 没有 Like
运算符。
这是在纯 VBScript 中附加到具有特定标题的 IE window 的常用方法:
Set app = CreateObject("Shell.Application")
For Each wnd In app.Windows
If wnd.Name = "Internet Explorer" Then
If InStr(1, wnd.Document.Title, "Google", vbTextCompare) > 0 Then
Set ie = wnd
Exit For
End If
End If
Next
您可以通过 QTP 中的描述性编程来实现目标(如果您出于某种原因不想使用对象存储库)。这段代码应该给你一个你可以做什么的例子:
Dim oDesc ' create a Description object for objects of class Link
Set oDesc = Description.Create
oDesc("micclass").value = "Link"
'Find all the Links in the browser using ChildObjects
Set obj = Browser("title=Google").Page("title=Google").ChildObjects(oDesc)
Dim i
'obj.Count value has the number of links in the page
For i = 0 to obj.Count - 1 ' indexed from zero, so use 0 to Count -1
'get the name of all the links in the page
If obj(i).GetROProperty("innerhtml")= LinkHref Then
obj(i).Click 'click the link if it matched the href you specfied
Exit For ' no need to carry on the loop if we found the right link
End If
Next
如果你只是需要使用vbscript,你可以这样做:
Dim oShell : Set oShell = CreateObject("Shell.Application")
Dim oWindow
For Each oWindow In oShell.Windows
If InStr(oWindow.FullName, "iexplore") > 0 Then
If InStr(1, oWindow.Document.Title, "Google", vbTextCompare) > 0 Then
Set ieApp = oWindow
Exit For
End If
End If
Next
LinkHref = "//www.google.co.in/intl/en/about.html?fg=1"
For Each linky In ieApp.Document.GetElementsbyTagName("a")
If LCase(linky.GetAttribute("href")) = LCase(LinkHref) Then
linky.Click
Exit For
End If
Next
这几乎是 Ansgar 上面给出的答案,但有一些额外的内容来修复对象错误。只有浏览器 window 具有 Document.Title
,并且循环会遍历每个打开的 window,因此当循环尝试非 IE window 时会出现错误。如果 window 首先被识别为 IE 实例,此版本通过仅检查 Document.Title
来修复此问题。