VBA通过Internet Explorer,查找文本字符串并使用它

时间:2015-06-04 22:39:58

标签: vba excel-vba search excel

在我能用VBA打开的页面中;我想点击表格中的第一个链接,然后复制一些数据,关闭该窗口,点击表格的第二个链接等。

<a title="Open" 
  onmouseover="javascript:window.status='Click here!';return true;"
  onmouseout="javascript:window.status='';return true;" 
  onclick="OpenWindow(this.href, 'Profile_5193622', 760, 565); return false;" 
  href="/Pages/Popups/Profile.aspx?pid=5193622">

我想在URL中使用“/Pages/Popups/Profile.aspx?pid=5193622”并导航到它。我该如何解决这个问题?

你必须点击几个地方才能到达那里。这将带您到那里:

Sub ExtractFirstLeagueData()


Dim IE As Object, obj As Object
Dim League As Object
Dim links, link
Dim dict As Object




Set IE = CreateObject("internetexplorer.application")

IE.Visible = True
IE.navigate ("http://whatifsports.com/hbd/Pages/Main/WorldRedirect.aspx?id=3")

WaitFor IE

IE.navigate ("http://whatifsports.com/HBD/Pages/World/PlayerSearch.aspx")

WaitFor IE

IE.document.getelementsbyname("ctl00$ctl00$ctl00$Main$PageOptionsPlaceHolder$PageOptionsPlaceHolder$lNameTextBox")(0).Value = "a"

IE.document.getelementsbyname("ctl00$ctl00$ctl00$Main$PageOptionsPlaceHolder$PageOptionsPlaceHolder$LevelDropDown$LevelDropDown")(0).Value = "5"

IE.document.forms(0).submit

WaitFor IE

Set dict = CreateObject("scripting.dictionary")

WaitFor IE

'collect the player links
Set links = IE.document.getElementsByTagName("a")

WaitFor IE

For Each link In links
If link.href Like "*/Popups/PlayerProfile.aspx?pid=*" Then
    dict.Add link.innertext, link.href
End If
Next link

'navigate to each page and collect info
For Each link In dict.keys
IE.navigate dict(link)
WaitFor IE
Debug.Print IE.document.URL
'get player info here...
Next link
End Sub

Sub WaitFor(IE As Object)
While IE.readyState <> 4
    DoEvents
Wend
End Sub

1 个答案:

答案 0 :(得分:0)

Dim links, link
Dim dict As Object

'-------------------------
'get to the right page...
'-------------------------

Set dict = CreateObject("scripting.dictionary")

'collect the player links
Set links = IE.document.getElementsByTagName("a")
For Each link In links
    If link.href Like "*/Popups/PlayerProfile.aspx?pid=*" Then
        dict.Add link.innerText, link.href
    End If
Next link

'navigate to each page and collect info
For Each link In dict.keys
    IE.navigate dict(link)
    WaitFor IE
    Debug.Print IE.document.URL
    'get player info here...
Next link

助手子:

Sub WaitFor(IE As Object)
    While IE.readyState <> 4
        DoEvents
    Wend
End Sub