当前位置: 首页 > news >正文

用AppleScript点击无效,继续用pyautogui.click()

目标:点击下图中 CheckBox 元素

在这里插入图片描述

第一步:获取这个元素的位置,并打印出value,确认是开关是关的(value=0)再继续

set targetbox to checkbox 1 of group 1 of scroll area 1 of scroll area 1 of group 1 of group 1 of sheet 1 of window "Login Items & Extensions"set theValue to value of targetbox

在这里插入图片描述

第二步:点击目标元素

1. 用click 事件, 不生效,也无报错

set targetbox to checkbox 1 of group 1 of scroll area 1 of scroll area 1 of group 1 of group 1 of sheet 1 of window "Login Items & Extensions"if targetbox exists thenclick targetbox

2. 用perform action “AXPress”, 不生效,也无报错

perform action "AXPress" of targetbox

3. 用osascript 执行AppleScript,报错10006, 猜测是安全限制问题

do shell script "osascript -e 'tell application \"System Events\" to tell process \"System Extensions (Login Items (System Settings))\" to set value of checkbox \"checkbox 1 of group 1 of scroll area 1 of scroll area 1 of group 1 of group 1 of sheet 1\" of window \"Login Items & Extensions\" to 1'"

4. 直接改value 从0 → 1 , 不生效,也无报错

set value of targetbox to 1
5. 点击元素的坐标,无报错,不生效
trytell checkbox 1 of group 1 of scroll area 1 of scroll area 1 of group 1 of group 1 of sheet 1 of window "Login Items & Extensions"set {xPosition, yPosition} to positionset {xSize, ySize} to sizeend tell-- modify offsets if hot spot is not centered:click at {xPosition + (xSize div 2), yPosition + (ySize div 2)}return {xPosition + (xSize div 2), yPosition + (ySize div 2)}end try

6. 尝试点击其他按钮
点击同层级按钮的坐标,也不行

点击 上上上上层按钮【Done】,可以点也就是说 Done 这层是可以操作的,Done兄弟的下下下下一层就不能操作了

在这里插入图片描述

以下是我的猜测:
从UI Browser中看到这个element 不能显示树状结构,且Application 也变成了 “LoginItems” 而非一开始的“Systems Settings”,所以是到了某个加密的元素了。
Open image-20240812-060324.png
AppleScript 使用 macOS 提供的 API 来控制和自动化 macOS 应用程序。所以API不会破解macOS的加密元素。
而pyautogui 是跨平台的 GUI 自动化,尤其是基于屏幕坐标的操作,所以在AppleScript获取到元素位置后,再尝试用pyautogui.click()事件。

方法一:写click.py,其中包含pyautogui.click()方法,然后直接在AppleScript中run python click.py

方法二:在python文件中运行 AppleScript(以下方法中还包含根据元素名称点击元素的下一个button)

    def enable_macOS15_extension(self,software_name):log.info("enabling %s", software_name)applescript = f"""if major_version  is greater than or equal to 15 thendelay 10-- 获取目标窗口set targetWindow to window "{self.get_L10N_text("LoginItems_Extensions")}"-- 获取目标元素的所有兄弟元素(同层次元素)set siblingElements to UI elements of group 3 of scroll area 1 of group 1 of group 2 of splitter group 1 of group 1 of targetWindow-- 初始化变量set targetElementFound to falseset nextElement to missing value-- 遍历所有同层次元素,找到目标元素并获取其下一个元素repeat with i from 1 to count of siblingElementsset currentElement to item i of siblingElementsif value of currentElement contains "{software_name}" thenset targetElementFound to trueelse if targetElementFound thenset nextElement to currentElementexit repeatend ifend repeat-- 点击目标元素的下一个元素的信息if nextElement is not missing value thenclick nextElementdelay 3elsereturn "not found Antivirus Extension button"end if--点击弹窗中的CheckBoxset targetbox to checkbox 1 of group 1 of scroll area 1 of scroll area 1 of group 1 of group 1 of sheet 1 of targetWindowif targetbox exists thenif value of targetbox is 0 thentrytell checkbox 1 of group 1 of scroll area 1 of scroll area 1 of group 1 of group 1 of sheet 1 of targetWindowset {{xPosition, yPosition}} to positionset {{xSize, ySize}} to sizeend tell-- modify offsets if hot spot is not centered:return {{xPosition + (xSize div 2), yPosition + (ySize div 2)}}end tryelsereturn "CheckBox value is 1"end ifelsereturn "Not Found CheckBox"end ifelsereturn "Not macOS15" end if                """wrappedscript = self.wrapper(applescript)output = run_applescript(wrappedscript)checkbox_1 = output.split(",")x_position = int(checkbox_1[0])y_position = int(checkbox_1[1])log.info("CheckBox button at %s, %s",x_position, y_position)pyautogui.click(x=x_position,y=y_position)time.sleep(10)
http://www.lryc.cn/news/425192.html

相关文章:

  • 谈谈我用MemFire Cloud开发应用的这一两年
  • AI安全-文生图
  • Hibernate 使用详解
  • 乐普医疗校招社招笔试/测评通关攻略、最新北森题库、可搜索答案
  • uniapp在线下载安装包更新app
  • Unity | AmplifyShaderEditor插件基础(第一集:简单了解ASE和初识)
  • Windows文件资源管理器未响应,磁盘状态正常,很可能是这个原因
  • 良好的代码习惯
  • 音乐生成模型应用
  • DBEUG:二维图尺寸没思路
  • 【图像去雾系列】使用SSR/MSR/MSRCR/MSRCP/automatedMSRCR算法对单图像进行图像增强,达到去雾效果
  • oracle普通导出导入
  • 如何将CSDN文章导出为pdf文件
  • 利用Python实现供应链管理中的线性规划与资源优化——手机生产计划1
  • Spring Cloud全解析:配置中心之springCloudConfig分布式配置动态刷新
  • mac如何查看shell是 zsh还是bash
  • STM32cubeMX配置Systick的bug
  • 分享几个好用js片段
  • web前端之实现一只可爱的小杰尼乌龟、伪元素、动画
  • 银河麒麟服务器版在rc.local使用ifcong 配置IP和nmcli的区别
  • 【运维】深入理解 Linux 中的 `mv` 命令,使用 `mv` 移动所有文件但排除特定文件或文件夹
  • Xilinx课程,就这么水灵灵地上线了~
  • 【axios get请求 中文乱码】
  • 智能分析/视频汇聚EasyCVR安防视频融合管理云平台技术优势分析
  • arcgis-坡度坡向分析
  • 【银河麒麟高级服务器操作系统】实际案例分析,xfsaild占用过高
  • JS中【setTimeout】使用注意事项总结
  • 已解决ArkTS开发webview,html页面中的input和按钮等操作均无响应
  • ChatGPT无法登录,提示我们检测到可疑的登录行为,将阻止进一步的尝试。请与管理员联系
  • 【数据结构篇】~复杂度