【问题标题】:Hiding the CMD window of PhantomJS on python with selenium用 selenium 在 python 上隐藏 PhantomJS 的 CMD 窗口
【发布时间】:2023-03-15 21:05:01
【问题描述】:

我正在尝试隐藏使用 python 启动 PhantomJS 时出现的 CMD 窗口:

from selenium import webdriver
browser = webdriver.PhantomJS()

在使用 C# 时似乎有一个 solution,但是,我找不到与 python 类似的任何东西。由于 C# 的功能是在 2014 年添加的,我认为 python 也应该存在类似的东西。

我正在使用最新的 PhantomJS 和 Python 3.6.2。

看起来是这样的:

【问题讨论】:

  • 也许这可以帮助你stackoverflow.com/questions/25871898/…
  • 也许我不明白您是如何启动代码的,但是当我使用 PhantomJS 时,我没有打开额外的 cmd 窗口。当然,我确实有原始的 cmd 窗口,这是我用来运行脚本的那个窗口。如果我通过单击 python 文件启动脚本,它将弹出一个 cmd 窗口,但这只是一个常规 cmd,与 phantomjs 无关。
  • 你能给我们看一张 CMD 窗口的快照吗?
  • 为清晰起见添加了屏幕截图

标签: selenium-webdriver phantomjs python-3.6


【解决方案1】:

对我来说, 下面的代码工作正常。

driver = webdriver.PhantomJS(
    service_args=service_args,
    desired_capabilities=caps,
    executable_path='phantomjs.exe',
    service_log_path=None
)

您必须在此处添加 service_log_path=None 以表示没有日志文件,这就是控制台窗口将被隐藏的原因。

还是没有解决

替换代码

C:\Users\YOUR_USER_NAME\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\selenium\webdriver\common\service.py)文件

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

self.process = subprocess.Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=False, creationflags=0x08000000)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-29
    • 1970-01-01
    • 2014-01-09
    • 2022-01-04
    • 1970-01-01
    • 2017-08-07
    • 2018-01-14
    • 2021-07-12
    相关资源
    最近更新 更多