首页 > 解决方案 > 如何使用 python、selenium 在 Mac 上设置焦点?

问题描述

if platform == "darwin": #if on OS X, requires this module: pip install pygetwindow
    import pygetwindow as gw
    titles = gw.getAllTitles()
    print(titles)
    win = gw.getWindowsWithTitle('Photoshop')
    win.activate()

这是终端中的结果:

['SystemUIServer ', 'Control Centre ', 'Control Centre ', 'Spotlight ', 'Control Centre ', 'SystemUIServer ', 'Control Centre ', 'Window Server Menubar', 'Dock ', 'Photoshop ', 'Terminal ', 'IDLE ', 'Finder ', 'Finder ']
Exception in Tkinter callback
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/tkinter/__init__.py", line 1892, in __call__
    return self.func(*args)
  File "/Users/username/Library/Mobile Documents/com~apple~CloudDocs/test.py", line 1237, in clicker1
    week_or_month()
  File "/Users/username/Library/Mobile Documents/com~apple~CloudDocs/test.py", line 899, in week_or_month
    window_focus()
  File "/Users/username/Library/Mobile Documents/com~apple~CloudDocs/test.py", line 680, in window_focus
    win = gw.getWindowsWithTitle('Photoshop')
AttributeError: module 'pygetwindow' has no attribute 'getWindowsWithTitle'

有没有办法解决这个问题并专注于窗口“Photoshop”?函数 gw.getAllTitles() 有效。

我也试过这个(如另一篇文章中所述),结果相同:

win = pyautogui.getWindowsWithTitle("Photoshop")

结果:

AttributeError: module 'pyautogui' has no attribute 'getWindowsWithTitle'

标签: pythonselenium

解决方案


这是因为您使用了错误的调用,请尝试查看文档中的文档或按照我的示例进行操作。

你的错误:

AttributeError: module 'pygetwindow' has no attribute 'getWindowsWithTitle'

尝试

pygetwindow.getWindowsWithTitle('Photoshop') # I dont understand why you need to index into a "list"

另外,如果这不起作用,请尝试该gw.getAllTitles()方法。 ('', 'C:\\WINDOWS\\system32\\cmd.exe - pipenv shell - python'")代替(Win32Window(hWnd=131318), Win32Window(hWnd=1050492), Win32Window(hWnd=67206))


推荐阅读