【问题标题】:Why pyautogui cannot find items in OSX Dock?为什么 pyautogui 在 OSX Dock 中找不到项目?
【发布时间】:2018-09-11 22:11:47
【问题描述】:

我开始为我的个人项目学习 pyautogui,但在尝试打开 OSX 停靠图标时几乎立即遇到了问题。

我想打开 Mac Launchpad 下的本地 Spotify。

我的代码来做到这一点。

import pyautogui

launchpad = pyautogui.locateOnScreen('img/Launchpad.png')

这个返回None所以图片没有找到。

附上图片示例

但是,如果我打开 Mac OSX Notes 窗口并将相同的图像粘贴到其中并再次运行该程序,则每次都会找到该图像。同样,如果我只是在我的编辑器中打开图像。

dock 实际上是 pyautogui 可以搜索的 OSX 屏幕的一部分吗?如果不是如何与之交互?

【问题讨论】:

    标签: python macos pyautogui


    【解决方案1】:

    认为使用应用程序热键与在屏幕上查找是一种不那么脆弱的方法。下面是我最终构建 Spotify 机器人的方式。

    import time
    
    import pyautogui
    
    # use pyauutogui key shortcut to open OSX spotlight search
    pyautogui.hotkey('command', 'space')
    # type spotify and press enter to open application
    pyautogui.typewrite('Spotify')
    pyautogui.hotkey('enter')
    # use Spotify keyboard shortcuts to select search.
    # key docs here: https://support.spotify.com/ie/article/Keyboard-shortcuts/
    time.sleep(5)
    pyautogui.hotkey('command', 'l')
    # typewrite allows passing string arguments using keyboard
    pyautogui.typewrite('concentration music')
    # move to select the song with tab and press enter to play
    pyautogui.hotkey('tab', 'tab', 'tab', 'tab')
    time.sleep(2)
    pyautogui.hotkey('enter')
    pyautogui.hotkey('space')
    # sleeps 30 seconds while music is playing
    time.sleep(30)
    pyautogui.hotkey('command', 'q')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-13
      • 2016-07-22
      • 1970-01-01
      • 1970-01-01
      • 2011-02-07
      • 2019-01-24
      • 1970-01-01
      相关资源
      最近更新 更多