2016-06-07 140 views
1

在下面的代码中,我尝试单击www.google.co.in网站上的“关于”链接(href)。这适用于IE11(Windows 10),但不适用于IE10(Windows 7)。这是否依赖于机器。如果不是什么是正确的代码?单击已打开的浏览器窗口中的href链接

请记住我试图点击已打开的浏览器窗口中的链接。

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 
+1

如果您使用的是QTP,也许使用内置的浏览器自动化和一些描述性编程会更简单,甚至只是使用对象库来了解您的目标站点并导航它?最好的方法将取决于你的脚本的总体目标,虽然 – Dave

+1

什么是__如果my_title像“谷歌”和“*”然后'__Like__? – SearchAndResQ

+0

奇怪....但它确实工作,因为我在我的Win10 IE11浏览器上说过 –

回答

2

您可以用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,但有一点额外的修复对象错误。只有浏览器窗口具有Document.Title,并且循环正在处理每个打开的窗口,因此当循环尝试一个非IE窗口时会出现错误。该版本通过仅检查Document.Title来确定该窗口是否首先被识别为IE实例。

+0

非常感谢戴夫给你的答案,但这可以单独在VBScript中完成吗? –

+0

我已经为我的答案添加了一个vbscript方法 – Dave

+0

我认为这不是找到ie窗口...因为它说的对象所需的“ieApp” –

1

不知道QTP,但VBScript没有Like运算符。

这是附加到一个IE窗口与普通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 
+0

QTP/UFT没有'Like'操作符... – Dave

+0

@Ansar Wiechers它说'对象不支持这个属性或方法'“wnd.Document.Title” –

+0

您可能已打开(文件)资源管理器窗口。查看更新的答案。 –

相关问题