You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过WinAppDriver和Appium在远程机器点击Windows应用指定坐标?

远程Windows应用坐标点击问题解决方案
  • 检查远程WinAppDriver的配置与权限
    远程机器上的WinAppDriver必须以管理员身份启动,同时要开放默认端口4723的防火墙规则。启动时需指定允许所有IP访问:
    WinAppDriver.exe 0.0.0.0 4723
    这是远程连接的基础前提,不少失败案例都源于此配置缺失。

  • 用TouchAction执行应用内坐标点击
    这是Appium官方推荐的操作方式,注意此处坐标是相对于应用窗口的(而非全局屏幕)。Java示例代码:

    TouchAction touchAction = new TouchAction(driver);
    touchAction.tap(PointOption.point(targetX, targetY)).perform();
    

    若需使用全局屏幕坐标,先获取应用窗口的左上角偏移量,计算出相对坐标:

    Point windowLocation = driver.manage().window().getPosition();
    int relativeX = screenX - windowLocation.getX();
    int relativeY = screenY - windowLocation.getY();
    
  • 调用Windows原生API实现全局点击
    如果TouchAction无效,可直接通过PowerShell调用user32.dll模拟鼠标点击,这是全局操作最可靠的方式。先准备PowerShell脚本:

    Add-Type @"
    using System;
    using System.Runtime.InteropServices;
    public class MouseSim {
        [DllImport("user32.dll")]
        public static extern bool SetCursorPos(int X, int Y);
        [DllImport("user32.dll")]
        public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);
        private const uint MOUSEEVENTF_LEFTDOWN = 0x02;
        private const uint MOUSEEVENTF_LEFTUP = 0x04;
        public static void LeftClick(int x, int y) {
            SetCursorPos(x, y);
            mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, (uint)x, (uint)y, 0, 0);
        }
    }
    "@
    [MouseSim]::LeftClick(150, 300) # 替换为你的目标坐标
    

    再通过Appium的shell命令在远程机器执行:

    String psScript = "上面的完整PowerShell代码";
    driver.executeScript("mobile: shell", Map.of("command", "powershell -Command \"" + psScript + "\""));
    
  • 验证坐标的准确性
    远程机器的屏幕分辨率可能和本地不同,导致坐标偏移。可以:

    • 用Appium获取远程屏幕尺寸:driver.manage().window().getSize()
    • 在远程机器上打开画图工具,点击目标位置,查看状态栏显示的坐标值,确保代码中使用的坐标正确。
  • 确认会话上下文正确
    确保当前Appium会话绑定的是目标Windows应用,没有切换到其他上下文:

    System.out.println("当前上下文: " + driver.getContext());
    

    如果上下文错误,切换回应用的原生上下文即可。

内容的提问来源于stack exchange,提问作者Aidin Shahmoradi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 03:23:11