Selenium-manager.exe窗口弹出

huangapple go评论117阅读模式
英文:

Selenium-manager.exe window popping up

问题

在尝试使用Selenium设置一个无头浏览器时,会出现一个控制台窗口短暂弹出然后关闭的情况,这不是chromedriver.exe窗口,因为它由CREATE_NO_WINDOW处理,这个窗口被称为selenium-manager.exe。

只有当我使用Pyinstaller将脚本构建为可执行文件时才会出现这个窗口,因为当我正常使用Python运行时它不会出现。我使用的命令是pyinstaller --windowed my_script.py,我还尝试添加--noconsole和单独使用noconsole,但都没有起作用。是什么原因导致这个问题,我该如何解决?

操作系统:Windows-10-10.0.19045-SP0,Python版本:3.11.1,Selenium版本:4.10.0,Pyinstaller版本:5.13.0

以下是代码部分,不需要翻译:

from subprocess import CREATE_NO_WINDOW
from selenium.webdriver.chrome.service import Service
from selenium.webdriver import Chrome, ChromeOptions

def setup_options(options: ChromeOptions) -> ChromeOptions:
    options.add_argument('--headless=new')
    options.add_argument('--disable-extensions')
    options.add_argument('--disable-infobars')
    options.add_argument('--no-sandbox')
    return options

def setup_driver() -> Chrome:
    service_chrome = Service()
    service_chrome.creation_flags = CREATE_NO_WINDOW
    options = setup_options(ChromeOptions())
    return Chrome(service=service_chrome, options=options)

driver = setup_driver()
# Do some automated work
driver.quit()

这是窗口的截图:

Selenium-manager.exe窗口弹出

英文:

While trying to setup a headless browser using selenium a console window pops up briefly before it closes, it's not the chromedriver.exe window cause that's handled by CREATE_NO_WINDOW the window is called selenium-manager.exe.

It only pops up when I build my script into an executable with Pyinstaller cause when I run it normally with Python it doesn't appear. The command I used was pyinstaller --windowed my_script.py, I also tried adding --noconsole and using noconsole alone but nothing worked. What could be causing this and how do I fix it?

Operation System: Windows-10-10.0.19045-SP0, Python: 3.11.1, Selenium: 4.10.0, Pyinstaller: 5.13.0

from subprocess import CREATE_NO_WINDOW
from selenium.webdriver.chrome.service import Service
from selenium.webdriver import Chrome, ChromeOptions

def setup_options(options: ChromeOptions) -> ChromeOptions :
    options.add_argument('--headless=new')
    options.add_argument('--disable-extensions')
    options.add_argument('--disable-infobars')
    options.add_argument('--no-sandbox')
    return options

def setup_driver() -> Chrome:
    service_chrome = Service()
    service_chrome.creation_flags = CREATE_NO_WINDOW
    options = setup_options(ChromeOptions())
    return Chrome(service=service_chrome, options=options)

driver = setup_driver()
# Do some automated work
driver.quit()

Here's a screenshot of the window:

Selenium-manager.exe窗口弹出

答案1

得分: 2

Hacky修复方法,进入你的虚拟环境中的以下路径:".venv\lib\site-packages\selenium\webdriver\common\selenium_manager.py"。

在脚本中,有一个名为SeleniumManager类中的run方法,它启动一个subprocess来执行selenium-manager.exe。他们犯了一个错误,假设每个人都在控制台环境下运行脚本,或者可能没有考虑到Windows用户,所以在调用subprocess.run时,他们没有添加creationflags=CREATE_NO_WINDOW

将其编辑如下:

            if sys.platform == "win32":
                completed_proc = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, creationflags=subprocess.CREATE_NO_WINDOW)
            else: 
                completed_proc = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

请注意,这个错误只存在于Windows中。他们可能会在下一个更新中解决这个问题。我已经合并了一个修复的pull request

英文:

Hacky fix, navigate to the following path in your virtual environment ".venv\lib\site-packages\selenium\webdriver\common\selenium_manager.py".

Within the script, there's a method called run in the SeleniumManager class that starts a subprocess that executes the selenium-manager.exe they made an over-sight and assumed everyone would be running their scripts in a console environment or maybe didn't consider windows users, so when making the call to subprocess.run they didn't add creationflags=CREATE_NO_WINDOW.

Edit it as follows


            if sys.platform == "win32":
                completed_proc = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, creationflags=subprocess.CREATE_NO_WINDOW)
            else: 
                completed_proc = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

Note that the bug is only present in windows. They'll probably address it in the next updates. I made merged pull request with the fix.

答案2

得分: 1

我们可以通过在启动Chromeservice时明确引用Chromedriver来避免这个问题。

在这个例子中,我发现路径需要是绝对路径,而不是相对路径。也许你可以尝试使用相对路径来使其工作。

from subprocess import CREATE_NO_WINDOW
from selenium.webdriver.chrome.service import Service
from selenium.webdriver import Chrome, ChromeOptions

import sys
import os

def get_path(relative_path):
    """获取资源的绝对路径,适用于开发和PyInstaller"""
    try:
        # PyInstaller会创建一个临时文件夹,并将路径存储在_MEIPASS中
        base_path = sys._MEIPASS
    except Exception:
        base_path = os.path.abspath(".")
    return os.path.join(base_path, relative_path)

def setup_options(options: ChromeOptions) -> ChromeOptions:
    options.add_argument('--headless=new')
    options.add_argument('--disable-extensions')
    options.add_argument('--disable-infobars')
    options.add_argument('--no-sandbox')
    return options


def setup_driver() -> Chrome:
    service_chrome = Service((get_path('C:/absolute/path/to/driver/chromedriver.exe')))
    service_chrome.creation_flags = CREATE_NO_WINDOW
    options = setup_options(ChromeOptions())
    return Chrome(service=service_chrome, options=options)


driver = setup_driver()
# 进行一些自动化工作
driver.quit()
英文:

We can avoid this by referring to the Chromedriver explicitly when starting the Chromeservice.

In this example, I found the path needs to be an absolute path, not a relative one. Maybe you can probably get it to work with a relative path

from subprocess import CREATE_NO_WINDOW
from selenium.webdriver.chrome.service import Service
from selenium.webdriver import Chrome, ChromeOptions

import sys
import os

def get_path(relative_path):
    """ Get the absolute path to the resource, works for dev and for PyInstaller """
    try:
        # PyInstaller creates a temp folder and stores path in _MEIPASS
        base_path = sys._MEIPASS
    except Exception:
        base_path = os.path.abspath(".")
    return os.path.join(base_path, relative_path)

def setup_options(options: ChromeOptions) -> ChromeOptions:
    options.add_argument('--headless=new')
    options.add_argument('--disable-extensions')
    options.add_argument('--disable-infobars')
    options.add_argument('--no-sandbox')
    return options


def setup_driver() -> Chrome:
    service_chrome = Service((get_path('C:/absolute/path/to/driver/chromedriver.exe')))
    service_chrome.creation_flags = CREATE_NO_WINDOW
    options = setup_options(ChromeOptions())
    return Chrome(service=service_chrome, options=options)


driver = setup_driver()
# Do some automated work
driver.quit()

huangapple
  • 本文由 发表于 2023年7月27日 16:49:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76778041.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定