在Selenium ChromeDriver中隐藏命令提示符 [英] Hide command prompt in Selenium ChromeDriver

查看:451
本文介绍了在Selenium ChromeDriver中隐藏命令提示符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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中复制以上代码的方法(或多或少) 使用了此修复 作为我灵感的基础.

经过数小时的挣扎(也确实是不好的话),我做了这个 在github上提交 ,现在可以通过代码轻松更改控制台提示行为. 我将尝试在官方资源中提供它.希望它可以节省您的时间和耐心.

步骤1

通常在"X:\ YourPythonFold \ Lib \ site-packages \ selenium \ webdriver \ common \ service.py"中找到 service.py

STEP 2

替换这些行(大约n°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!不要不要使用制表符,只能使用空格!

var driverService = ChromeDriverService.CreateDefaultService();
driverService.HideCommandPromptWindow = true;

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

Is it possible to achieve this in Python? I've tried all the possible fixes posted here on StackoverFlow and outside, but nothing when I run my .exe, the cmd appears too.

Python 3.6, latest Selenium version (3.9).

解决方案

I found the way to replicate the above code in python (more or less) Used this fix as base to my inspiration.

After hours of struggling (and also really bad words), I've made this commit on github bywhich console prompt behaviour is now easily changable via code. I will try to make it available in the official sources. Hope it will make you save time and patience.

STEP 1

Locate service.py, generally in "X:\YourPythonFold\Lib\site-packages\selenium\webdriver\common\service.py"

STEP 2

Replace these lines (n° 72-76 approximately, below start method def):

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

with

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)

Finally in your code, when you setup your driver (I chose Chrome as example):

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

When edit the source code, be careful to PEP! Do not use tabs, just spaces!

这篇关于在Selenium ChromeDriver中隐藏命令提示符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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