在Selenium ChromeDriver中隐藏命令提示符

问题描述 投票:2回答:1
var driverService = ChromeDriverService.CreateDefaultService();
driverService.HideCommandPromptWindow = true;

var driver = new ChromeDriver(driverService, new ChromeOptions());

是否有可能在Python中实现这一点?我已经尝试了在StackoverFlow和外面发布的所有可能的修复,但是当我运行我的.exe时,没有任何东西,cmd也出现了。

Python 3.6,最新的Selenium版本(3.9)。

python selenium selenium-webdriver
1个回答
2
投票

我找到了在python中复制上述代码的方法(或多或少)使用这个fix作为我灵感的基础。

经过几个小时的挣扎(也是非常糟糕的话),我已经在github上制作了这个commit,现在可以通过代码轻松改变控制台提示行为。我将尽力在官方消息来源提供。希望它能让你节省时间和耐心。

步骤1

找到service.py,通常位于“X:\ YourPythonFold \ Lib \ site-packages \ selenium \ webdriver \ common \ service.py”中

第2步

更换这些线(约72-76,低于启动方法def):

self.process = subprocess.Popen(cmd, env=self.env,
                                            close_fds=platform.system() != 'Windows',
                                            stdout=self.log_file,
                                            stderr=self.log_file,
                                            stdin=PIPE)

if any("hide_console" in arg for arg in self.command_line_args()):
                self.process = subprocess.Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE, creationflags=0x08000000)
            else:
                self.process = subprocess.Popen(cmd, env=self.env, close_fds=platform.system() != 'Windows', stdout=self.log_file, stderr=self.log_file, stdin=PIPE)

最后在您的代码中,当您设置驱动程序时(我选择Chrome作为示例):

args = ["hide_console", ]
driver = webdriver.Chrome("your-path-to-chromedriver.exe", service_args=args, ...)

编辑源代码时,请注意PEP!不要使用标签,只是空格!

© www.soinside.com 2019 - 2024. All rights reserved.