問題描述
我在 Selenium 中使用 chrome 驅(qū)動(dòng)程序打開 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.
這里是獲取驅(qū)動(dòng)的部分代碼:
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
當(dāng)我啟動(dòng)我的應(yīng)用程序時(shí),我得到一個(gè) chromedriver.exe 控制臺(tái)(一個(gè)黑色窗口),然后打開一個(gè) chrome 窗口,我的所有請(qǐng)求都已完成.
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 中有沒有辦法隱藏控制臺(tái)窗口?
My question: is there a way in python to hide the console window ?
(如您所見,我也在調(diào)整 chrome 窗口的大小,我的偏好是以用戶不會(huì)注意到屏幕上發(fā)生的任何事情的方式做事)
(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)
謝謝西萬(wàn)
推薦答案
您必須編輯 Selenium 源代碼才能實(shí)現(xiàn)此目的.我也是菜鳥,我不完全理解編輯源代碼的整體后果,但這是我在 Windows 7、Python 2.7 上隱藏 webdriver 控制臺(tái)窗口所做的工作.
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.
通過以下方式添加創(chuàng)建標(biāo)志來(lái)編輯 Start() 函數(shù):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
您必須添加相關(guān)的導(dǎo)入:
You will have to add the relevant imports:
from win32process import CREATE_NO_WINDOW
這也適用于 Chrome webdriver,因?yàn)樗鼈儗?dǎo)入相同的文件來(lái)啟動(dòng) webdriver 進(jìn)程.
This should also work for Chrome webdriver as they import the same file to start the webdriver process.
這篇關(guān)于在 python 中隱藏 chromeDriver 控制臺(tái)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!