简体   繁体   English

在第二台 MacBook Pro 桌面上执行 PyAutoGUI 代码

[英]Execute PyAutoGUI code on second MacBook Pro desktop

I have the following code, which matches a screenshot of the "Tech" header/button from the Wall Street Journal's front page, and then clicks on it -我有以下代码,它与华尔街日报首页的“技术”标题/按钮的屏幕截图相匹配,然后单击它-

import pyautogui as pya
import time

# timing start
start = time.time()

# center of screen
pya.moveTo(840, 525, 1)

# navigate to "Tech" button
p = pya.locateOnScreen('wsj-tech.png', confidence = 0.8, grayscale=False)
print(p)
x, y = pya.center(p)
x, y = x/2, y/2
d = pya.moveTo(x, y, 1)
pya.doubleClick(d)

# end timer
end = time.time()
print(end - start)

To execute the code, I have to set the IDE (spyder) above (below also works) the browser:要执行代码,我必须将 IDE (spyder) 设置为上面(下面也可以使用)浏览器:

在此处输入图片说明

Is there any way I can leave the browser on another desktop?有什么办法可以将浏览器留在另一个桌面上? Eg execute the script in spyder (on "Desktop 2"), and then swipe over to "Desktop 1" to see the autoclicker work on the WSJ page?例如,在 spyder 中(在“桌面 2”上)执行脚本,然后滑动到“桌面 1”以查看自动点击器在 WSJ 页面上的工作?

FYI the computer is not connected to any external desktops.仅供参考,计算机连接到任何外部桌面。

在此处输入图片说明

Great question!好问题! Unfortunately, it is not possible to do what you're asking with pyautogui .不幸的是,使用pyautogui无法完成您的要求。 This is because pyautogui can only work on foreground objects/windows and not minimized objects/windows in the background or in another workspace.这是因为pyautogui只能在前景对象/窗口上工作,而不能在后台或其他工作区中最小化对象/窗口。 It is a noted feature request , who knows if or when it might get implemented.这是一个值得注意的功能请求,谁知道它是否或何时可以实现。

Possible Solutions:可能的解决方案:

  • If you don't need the GUI, just use the python library selenium to find the WSJ banner and click on it with a headless chrome browser instance (browser will be hidden)如果不需要 GUI,只需使用pythonselenium找到 WSJ 横幅并使用无头chrome 浏览器实例单击它(浏览器将被隐藏)

  • Have your code & browser run in a virtual machine , so it can then execute without having to be in the foreground让您的代码和浏览器在虚拟机中运行,这样它就可以执行而无需在前台

  • Try python library atomacos which provides automation of macOS GUI applications尝试提供macOS GUI 应用程序自动化的pythonatomacos

  • Use a macOS automation tool like Hammerspoon which allows you to move windows, talk to other applications, etc使用像Hammerspoon这样的macOS自动化工具,它允许您移动窗口、与其他应用程序对话等

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM