In the below code I\'m trying to click on the \"About\" link (href
) in the www.google.co.in website. This worked on IE11 (Windows 10), but is not working for IE10 (
You can achieve the goal with descriptive programming in QTP (if you don't want to use the object repository for some reason). This code should give you an example of what you can do:
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
If you just need to use vbscript, you can do it like this:
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
This is pretty much the answer given above by Ansgar, but with a little extra to fix the object error. Only a browser window has the Document.Title
, and the loop is working through every window that's open, so you get the error when the loop tries a non IE window. This version fixes that by only checking for the Document.Title
if the window has been identified as an IE instance in the first place.
Don't know about QTP, but VBScript doesn't have a Like
operator.
This is the usual way to attach to an IE window with a specific title in plain VBScript:
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