vba 如何点击这个按钮?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15679859/
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 20:18:56  来源:igfitidea点击:

How to click this button?

vbabuttonclicksrc

提问by user2165404

Hi all I am a beginner in Visual Basic. Your help is highly appreciated.

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

How can i click this button in a webpage?

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

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

回答by dee

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

对我有用的简短示例,使用简单的 html 文件进行了测试:

ClickA.html:

点击A.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:

vba标准模块:

' 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

回答by Sohail Hameed

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)

它将重定向到“what_would_you_do.html 页面(如果它在根目录中)