2016-06-07 139 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

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

相關問題