在 python 中隐藏 chromeDriver 控制台 [英] hide chromeDriver console in python

查看:118
本文介绍了在 python 中隐藏 chromeDriver 控制台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Selenium 中使用 chrome 驱动程序打开 chrome,登录到路由器,按一些按钮,上传配置等.所有代码都是用 Python 编写的.

I'm using chrome driver in Selenium to open chrome , log into a router, press some buttons ,upload configuration etc. all code is written in Python.

这里是获取驱动的部分代码:

here is the part of the code to obtain the driver:

chrome_options = webdriver.ChromeOptions()
prefs = {"download.default_directory":  self.user_local}
chrome_options.add_experimental_option("prefs", prefs)
chrome_options.experimental_options.
driver = webdriver.Chrome("chromedriver.exe", chrome_options=chrome_options)
driver.set_window_position(0, 0)
driver.set_window_size(0, 0)

return driver

当我启动我的应用程序时,我得到一个 chromedriver.exe 控制台(一个黑色窗口),然后打开一个 chrome 窗口,我的所有请求都已完成.

when i fire up my app, i get a chromedriver.exe console (a black window) followed by a chrome window opened and all my requests are done.

我的问题:在 python 中有没有办法隐藏控制台窗口?

My question: is there a way in python to hide the console window ?

(如您所见,我也在调整 chrome 窗口的大小,我的偏好是以用户不会注意到屏幕上发生的任何事情的方式做事)

(as you can see i'm also re-sizing the chrome window ,my preference would be doing things in a way the user wont notice anything happening on screen)

谢谢西万

推荐答案

您必须编辑 Selenium 源代码才能实现此目的.我也是菜鸟,我不完全理解编辑源代码的整体后果,但这是我在 Windows 7、Python 2.7 上隐藏 webdriver 控制台窗口所做的工作.

You will have to edit Selenium Source code to achieve this. I am a noob too, and I dont fully understand the overall consequences of editing source code but here is what I did to achieve hiding the webdriver console window on Windows 7, Python 2.7.

找到并编辑此文件,如下所示:位于Libsite-packagesseleniumwebdrivercommonservice.py 在您的 Python 文件夹中.

Locate and edit this file as follows: located at Libsite-packagesseleniumwebdrivercommonservice.py in your Python folder.

通过以下方式添加创建标志来编辑 Start() 函数:creationflags=CREATE_NO_WINDOW

Edit the Start() function by adding the creation flags this way: creationflags=CREATE_NO_WINDOW

修改后的方法如下:

def start(self):
    """
    Starts the Service.

    :Exceptions:
     - WebDriverException : Raised either when it can't start the service
       or when it can't connect to the service
    """
    try:
        cmd = [self.path]
        cmd.extend(self.command_line_args())
        self.process = subprocess.Popen(cmd, env=self.env,
                                        close_fds=platform.system() != 'Windows',
                                        stdout=self.log_file, stderr=self.log_file, creationflags=CREATE_NO_WINDOW)
    except TypeError:
        raise

您必须添加相关的导入:

You will have to add the relevant imports:

from win32process import CREATE_NO_WINDOW

这也适用于 Chrome webdriver,因为它们导入相同的文件来启动 webdriver 进程.

This should also work for Chrome webdriver as they import the same file to start the webdriver process.

这篇关于在 python 中隐藏 chromeDriver 控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆