2

大家好,我是 Visual Basic 的初学者。非常感谢您的帮助。

如何在网页中单击此按钮?

<a class="buttonRed" href="what_would_you_do.html" onclick="this.blur();">
  <span>Get Started</span>
</a>
4

2 回答 2

2

Short example which worked for me, tested with simple html file:

ClickA.html:

<!DOCTYPE HTML>
<html lang="en">
<head>
    <title><!-- Insert your title here --></title>
</head>
<body>
    <a class="buttonRed" href="what_would_you_do.html" onclick="this.blur();">
    <span>Get Started</span>
</a>
</body>
</html>

vba standard module:

' Add References:
' - Microsoft HTML Object Library
' - Microsoft Internet Controls

Sub test()
      Dim Browser As InternetExplorer
      Dim Document As htmlDocument
      Set Browser = New InternetExplorer

      Browser.Visible = True
      Browser.navigate "C:\Temp\VBA\ClickA.html"

      Do While Browser.Busy And Not Browser.readyState = READYSTATE_COMPLETE
          DoEvents
      Loop

      Set Document = Browser.Document
      Dim anchorElement As HTMLAnchorElement
      Set anchorElement = Document.getElementsByClassName("buttonRed").Item(Index:=1)
      anchorElement.Click
      Set Document = Nothing
      Set Browser = Nothing
  End Sub
于 2013-03-28T11:41:36.623 回答
0

actually this is not a button. its anchor tag. write your code as

  <a class="buttonRed" href="what_would_you_do.html">
   <span>Get Started</span>
   </a>

and it will redirect to "what_would_you_do.html page (if its in root)

于 2013-03-28T11:34:45.290 回答