【问题标题】:Moving/minimizing/resizing the PhantomJS shell in Python's WebDriver在 Python 的 WebDriver 中移动/最小化/调整 PhantomJS shell
【发布时间】:2016-02-17 18:55:27
【问题描述】:

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

from selenium import webdriver b = webdriver.PhantomJS()

我希望 PhantomJS shell 一启动就自动隐藏。我错误地认为你可以通过b.set_window_position()b.set_window_size() 来做到这一点,但似乎这些调整了无头浏览器的设置,无论如何都是不可见的。有什么办法可以隐藏窗口吗?

【问题讨论】:

    标签: python selenium phantomjs


    【解决方案1】:

    我找到了解决办法!据我所知,我所问的问题是不可能像其他浏览器那样使用 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

    【讨论】:

    • 谢谢,我有点挣扎。这仍然是最好的方法吗?
    • 据我所知,是的。我可能做的唯一改变是在加载 PhantomJS 之前和之后创建一个现有窗口的列表,以便您可以确定哪些窗口是新的(如果您将有多个 PhantomJS 窗口,这很好)。
    猜你喜欢
    • 2011-03-20
    • 1970-01-01
    • 2019-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多