AttributeError:模块“pyautogui”没有属性“getWindowsWithTitle”

问题描述 投票:0回答:3

我正在编写一个与浏览器交互的机器人(不要说服我为 Selenium 或键盘等重写),它给了我一个错误。附近没有名为 pyautogui 的文件,我尝试将其命名为 main 和 python v3.8、3.9。在每种情况下都会收到此错误。 MacOS

/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/rubicon/objc/ctypes_patch.py:21: UserWarning: rubicon.objc.ctypes_patch has only been tested with Python 3.4 through 3.8. You are using Python 3.9.4. Most likely things will work properly, but you may experience crashes if Python's internals have changed significantly.
  warnings.warn(
Traceback (most recent call last):
  File "/Users/alina/PycharmProjects/ABOBA/main_miner_alienworlds.py", line 183, in <module>
    for i in pyautogui.getWindowsWithTitle('##### #######'):
AttributeError: module 'pyautogui' has no attribute 'getWindowsWithTitle'
import pyautogui

…
for j in pyautogui.getWindowsWithTitle('##### #######'):
    do something

pyautogui 与 pip3 一起安装

python macos pyautogui
3个回答
3
投票

您使用了错误的模块!

https://pypi.org/project/PyGetWindow/ 是包含

getWindowsWithTitle

的模块

2
投票

不幸的是,从版本 0.9.53 开始,pyautogui 的 Windows 功能仅适用于 Windows,请参阅下面来源中的 https://i.stack.imgur.com/HKtcC.png:- https://automatetheboringstuff.com/2e/chapter20/


0
投票

希望答案还不算太晚。

对于 OSX 用户,您可能需要使用 Quartz 和 AppKit。

一般来说,AppKit是内置的,所以你只需要通过以下方式安装Quartz:

pip install pyobjc

获取窗口信息:

import Quartz
from AppKit import NSWorkspace

# This function retrieves a list of all windows with their titles
def get_window_list():
    window_list = []
    window_info_list = Quartz.CGWindowListCopyWindowInfo(Quartz.kCGWindowListOptionOnScreenOnly, Quartz.kCGNullWindowID)
    for window_info in window_info_list:
        window_list.append(window_info)
    return window_list

# This function will look for a window with a specific title
def get_window_with_title(title):
    for window in get_window_list():
        window_title = window.get('kCGWindowName', 'No Title')
        if window_title == title:
            return window
    return None

# Replace 'Your Window Title' with the actual title of the window you're looking for
window_title = "xxx"
window = get_window_with_title(window_title)
print(window)

您将看到如下结果:

{
    kCGWindowAlpha = 1;
    kCGWindowBounds =     {
        Height = 851;
        Width = 515;
        X = 0;
        Y = 37;
    };
    kCGWindowIsOnscreen = 1;
    kCGWindowLayer = 0;
    kCGWindowMemoryUsage = 2160;
    kCGWindowName = xxx;
    kCGWindowNumber = 24901;
    kCGWindowOwnerName = Aethric;
    kCGWindowOwnerPID = 41114;
    kCGWindowSharingState = 1;
    kCGWindowStoreType = 1;
}

希望这有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.