溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

selenium+python如何去除啟動的黑色cmd窗口

發布時間:2021-08-12 12:55:31 來源:億速云 閱讀:695 作者:小新 欄目:開發技術

這篇文章主要為大家展示了“selenium+python如何去除啟動的黑色cmd窗口”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“selenium+python如何去除啟動的黑色cmd窗口”這篇文章吧。

其實 selenium啟動窗口的時候就是 使用了subprocess.Popen 啟動的驅動程序的,只要在啟動的時候加上啟動不顯示窗口的參數即可。

修改代碼 位于 D:\Python35\Lib\site-packages\selenium\webdriver\common\service.py 主要是 Service類的start函數

 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平臺
    ### 在windows平臺上就隱藏窗口
    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) ### 啟動驅動
   self.PID = self.process.pid ### 將cmd窗口的進程pid 保留 因為 窗口被隱藏了 所以在后續程序中必須考慮主控進程結束的時候必須結束掉 驅動cmd窗口進程
  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)

注意 在前面先導入 sys包

因為隱藏了驅動cmd窗口 所以 結束程序的時候 一定要做殺死驅動cmd窗口的動作

以上是“selenium+python如何去除啟動的黑色cmd窗口”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女