在已打开的浏览器窗口中单击href链接 [英] Click on a href link in an already opened browser window

查看:111
本文介绍了在已打开的浏览器窗口中单击href链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,我试图单击www.google.co.in网站上的关于"链接(href).这适用于IE11(Windows 10),但不适用于IE10(Windows 7).无论如何,这是否取决于机器.如果没有,正确的代码是什么?

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 (Windows 7). Is this in anyway machine dependent. If not what is the right code?

请记住,我试图在一个已打开的浏览器窗口中单击一个链接.

Please remember I'm trying to click on a link in an already opened browser window.

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中的描述性编程来实现此目标(如果您出于某些原因不想使用对象存储库).此代码应为您提供示例,说明您可以执行以下操作:

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

如果只需要使用vbscript,则可以这样操作:

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

这几乎是Ansgar上面给出的答案,但还有一些额外的操作可以解决对象错误.只有浏览器窗口具有Document.Title,并且循环正在打开的每个窗口中进行操作,因此,当循环尝试使用非IE窗口时,会出现错误.此版本通过仅检查Document.Title来解决此问题,如果该窗口首先被标识为IE实例.

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.

这篇关于在已打开的浏览器窗口中单击href链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆