vba 使用光标坐标点击网页

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

Click on webpage using cursor coordinates

vbaexcel-vbaexcel

提问by Gaurav Pawar

Can any one help me to click on a web page using cursor coordinates. Tip: Button don't have ID & name

任何人都可以帮助我使用光标坐标单击网页。提示:按钮没有 ID 和名称

回答by Ripster

Here is an example of moving the mouse and clicking using mouse_event:

这是使用mouse_event移动鼠标和单击的示例:

Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, _
                                              ByVal dx As Long, _
                                              ByVal dy As Long, _
                                              ByVal cButtons As Long, _
                                              ByVal dwExtraInfo As Long)

Private Const MOUSEEVENTF_MOVE = &H1          ' mouse move
Private Const MOUSEEVENTF_LEFTDOWN = &H2      ' left button down
Private Const MOUSEEVENTF_LEFTUP = &H4        ' left button up
Private Const MOUSEEVENTF_RIGHTDOWN = &H8     ' right button down
Private Const MOUSEEVENTF_RIGHTUP = &H10      ' right button up
Private Const MOUSEEVENTF_MIDDLEDOWN = &H20   ' middle button down
Private Const MOUSEEVENTF_MIDDLEUP = &H40     ' middle button up
Private Const MOUSEEVENTF_WHEEL = &H800       ' wheel button rolled
Private Const MOUSEEVENTF_ABSOLUTE = &H8000   ' absolute move

Private Type POINTAPI
    x As Long
    y As Long
End Type


Sub Click()
    Dim pt As POINTAPI
    Dim x As Long
    Dim y As Long

    '(0,0) = top left
    '(65535,65535) = bottom right
    x = 65535 / 2
    y = 65535 / 2

    LeftClick x, y
End Sub

Sub LeftClick(x As Long, y As Long)
    'Move mouse
    mouse_event MOUSEEVENTF_ABSOLUTE + MOUSEEVENTF_MOVE, x, y, 0, 0

    'Press left click
    mouse_event MOUSEEVENTF_ABSOLUTE + MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0

    'Release left click
    mouse_event MOUSEEVENTF_ABSOLUTE + MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
End Sub