Skip to main content
 首页 » 编程设计

python之在 Python 的 WebDriver 中移动/最小化/调整 PhantomJS shell 的大小

2025年05月04日36bluestorm

我正在使用 Python 和 selenium webdriver 来运行 PhantomJS。

从 selenium 导入 webdriver b = webdriver.PhantomJS()

我希望 PhantomJS shell 在启动时自动隐藏。我错误地认为您可以通过 b.set_window_position() 或 b.set_window_size() 来执行此操作,但似乎这些调整了 headless 浏览器的设置,这无论如何是不可见的。有什么办法可以隐藏窗口吗?

请您参考如下方法:

我找到了解决办法!据我所知,我所问的是不可能像其他浏览器那样使用 webdriver。但是,您可以使用其他模块来查看存在的窗口的名称,然后根据名称将它们最小化。

import win32gui, win32con 
 
toplist = [] 
winlist = [] 
 
def enum_callback(hwnd, results): 
    winlist.append((hwnd, win32gui.GetWindowText(hwnd))) 
 
(CODE THAT BRINGS UP THE PHANTOMJS WINDOW) 
 
win32gui.EnumWindows(enum_callback, toplist) 
phantom = [(hwnd, title) for hwnd, title in winlist if 'phantom' in title.lower()] 
phantom = phantom[0] 
win32gui.ShowWindow(phantom[0], win32con.SW_MINIMIZE) 

此答案归功于 ars .