温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

selenium+python如何去除启动的黑色cmd窗口

发布时间:2021-08-12 12:55:31 来源:亿速云 阅读:620 作者:小新 栏目:开发技术

这篇文章主要为大家展示了“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