其實(shí) selenium啟動(dòng)窗口的時(shí)候就是 使用了subprocess.Popen 啟動(dòng)的驅(qū)動(dòng)程序的,只要在啟動(dòng)的時(shí)候加上啟動(dòng)不顯示窗口的參數(shù)即可。
下面魔改開(kāi)始O(∩_∩)O哈哈~
修改代碼 位于 D:/Python35/Lib/site-packages/selenium/webdriver/common/service.py 主要是 Service類的start函數(shù)
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()) if 'win32' in str(sys.platform).lower(): ### 這里判斷是否是windows平臺(tái) ### 在windows平臺(tái)上就隱藏窗口 startupinfo = subprocess.STARTUPINFO() startupinfo.dwFlags = subprocess.CREATE_NEW_CONSOLE | subprocess.STARTF_USESHOWWINDOW startupinfo.wShowWindow = subprocess.SW_HIDE else: startupinfo = None self.process = subprocess.Popen(cmd, env=self.env, close_fds=platform.system() != 'Windows', stdout=self.log_file, stderr=self.log_file,startupinfo=startupinfo) ### 啟動(dòng)驅(qū)動(dòng) self.PID = self.process.pid ### 將cmd窗口的進(jìn)程pid 保留 因?yàn)?窗口被隱藏了 所以在后續(xù)程序中必須考慮主控進(jìn)程結(jié)束的時(shí)候必須結(jié)束掉 驅(qū)動(dòng)cmd窗口進(jìn)程 except TypeError: raise except OSError as err: if err.errno == errno.ENOENT: raise WebDriverException( "'%s' executable needs to be in PATH. %s" % ( os.path.basename(self.path), self.start_error_message) ) elif err.errno == errno.EACCES: raise WebDriverException( "'%s' executable may have wrong permissions. %s" % ( os.path.basename(self.path), self.start_error_message) ) else: raise except Exception as e: raise WebDriverException( "The executable %s needs to be available in the path. %s/n%s" % (os.path.basename(self.path), self.start_error_message, str(e))) count = 0 while True: self.assert_process_still_running() if self.is_connectable(): break count += 1 time.sleep(1) if count == 30: raise WebDriverException("Can not connect to the Service %s" % self.path)
注意 在前面先導(dǎo)入 sys包
因?yàn)殡[藏了驅(qū)動(dòng)cmd窗口 所以 結(jié)束程序的時(shí)候 一定要做殺死驅(qū)動(dòng)cmd窗口的動(dòng)作哦 !O(∩_∩)O!!
以上這篇selenium+python 去除啟動(dòng)的黑色cmd窗口方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持VEVB武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選