如何在Mac上使用python,selenium设置窗口焦点?

neskvpey  于 2023-04-04  发布在  Python
关注(0)|答案(3)|浏览(450)
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'
cl25kdpy

cl25kdpy1#

这是因为您使用了错误的调用,请尝试查看documentation上的文档或遵循我的示例。
您的错误:
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))

9o685dep

9o685dep2#

它似乎与Mac不兼容。activate()方法只传递而不从Github Code做任何事情。
除了getAllTitles、getActiveWindow和getWindowsAt方法之外,它似乎与mac不兼容。其余的方法要么是临时的,要么是不完整的。然而,由于它使用Quartz,我正在研究这一点。

w8f9ii69

w8f9ii693#

您尝试使用AppleScript命令来激活窗口:

import subprocess
app_name = "Photoshop"
cmd = f'osascript -e \'activate application "{app_name}"\''
subprocess.call(cmd, shell=True)

相关问题