vba 打开网页并单击特定选项卡

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17633400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 22:12:58  来源:igfitidea点击:

Open a webpage and click a particular tab

excelvba

提问by Ninja

I am trying to open a webpage on button click, go to a particular section, traverse across multiple pages upon next button arrow image click.

我试图在单击按钮时打开一个网页,转到特定部分,在单击下一个按钮箭头图像时遍历多个页面。

I have to extract information from this website. The HTML structure of page is:

我必须从这个网站提取信息。页面的HTML结构是:

<div>
 <div>
  <table class="A">
   <tbody>
    <tr>
     <td class="B">
      <div class="C">
       <ul id="D" class="E">
        <li id="W" class="T U"></li>
        <li id="X" class="T U "></li>
        <li id="Y" class="T U"><span class="SPANCLASSNAME">HEADER TAB CAPTION</span></li>
        <li id="Z" class="T U"></li>
       </ul>
      </div>
     </td>
    </tr>
   </tbody>
  </table>  
 </div>
</div>

All the data I need could be under any lielement. Here I have it under the third lielement.

我需要的所有数据都可以在任何li元素下。我把它放在第三个li元素下。

Private Sub Button_Click()
    Dim IE As Object    
    Set IE = CreateObject("InternetExplorer.Application")
    IE.Visible = True
    IE.Navigate "URL"

    Do
        DoEvents
    Loop Until IE.ReadyState = 4

    Set AllSpanElements = IE.Document.getElementsByClassName("SPANCLASSNAME")
    For Each span In AllSpanElements
        MsgBox span.innerText
        If span.innerText = "HEADER TAB CAPTION" Then
            MsgBox span.innerText
                span.Click  'DOES NOT WORK. WHAT SHOULD BE THE CODE HERE?
            Exit For
        End If
    Next    
    MsgBox "Operation Completed"
End Sub

I am able to open the webpage and the MsgBox displays the text from all the lielements and flow reaches till Operation Completed but the click action does not happen.

我能够打开网页,并且 MsgBox 显示来自所有li元素和流的文本,直到操作完成,但单击操作不会发生。

What code would send the click action to IE?

什么代码会将点击动作发送到 IE?

回答by Santosh

If the span has javascript event attached the below code might do the trick.

如果 span 附加了 javascript 事件,则下面的代码可能会起作用。

 span.FireEvent("click()")

回答by Andy G

I believe it would be

我相信会

span.onclick();
// or
span.fireEvent('onclick');

(without the semi-colons)

(没有分号)

AddedIf it is not a JS attached event, try

添加如果不是 JS 附加事件,请尝试

span.click()

回答by TheSilkCode

I wasn't able to test it without knowing the URL, but below is the code I came up with:

我无法在不知道 URL 的情况下对其进行测试,但以下是我想出的代码:

Public Sub sampleCode()
Dim IE As InternetExplorer
Dim HTMLDoc As HTMLDocument
Dim AllSpanElements As IHTMLElementCollection
Dim spanCounter As Long
Dim targetSpan As HTMLObjectElement

Set IE = New InternetExplorer
With IE
    .Visible = True
    .Navigate "URL"
    Do While .Busy Or .ReadyState = READYSTATE_LOADING: Wend
    Set HTMLDoc = .document
End With

Set AllSpanElements = HTMLDoc.getElementsByTagName("li")
For spanCounter = 0 To AllSpanElements.Length - 1
    With AllSpanElements(spanCounter)
        MsgBox (.innerText)
        If .innerText = "HEADER TAB CAPTION" Then
            MsgBox (.innerText)
            .Click
            Exit For
        End If
    End With
Next

IE.Quit
Set IE = Nothing
MsgBox "Operation Completed"

End Sub

Hope this helps, TheSilkCode

希望这会有所帮助,TheSilkCode