【问题标题】:Make exe file from python selenium tests从 python selenium 测试制作 exe 文件
【发布时间】:2014-02-01 15:01:36
【问题描述】:

我尝试在 exe 文件中构建我的 python selenium 测试并在许多机器上运行它以保持测试独立于环境。但结果 *.exe 文件找不到 selenium webdriver。如何在 *.exe 文件中包含所有 selenium 依赖项?或者也许还有其他方法? 是否可以制作虚拟环境并分发?

【问题讨论】:

    标签: python selenium pyinstaller


    【解决方案1】:

    我假设您使用 py2exe 来生成 exe。您需要在 setup.py 文件中指定 selenium webdriver 的位置。

    以下代码应该会有所帮助:

    from distutils.core import setup
    import py2exe
    
    # Change the path in the following line for webdriver.xpi
    data_files = [('selenium/webdriver/firefox', ['D:/Python27/Lib/site-packages/selenium/webdriver/firefox/webdriver.xpi'])]
    
    setup(
        name='General name of app',
        version='1.0',
        description='General description of app',
        author='author name',
        author_email='author email',
        url='',
        windows=[{'script': 'abc.py'}],   # the main py file
        data_files=data_files,
        options={
            'py2exe':
                {
                    'skip_archive': True,
                    'optimize': 2,
                }
        }
    )
    

    【讨论】:

    • 谢谢它的工作,但我有太多的文件。是否可以将 webdriver_prefs.json 和 webdriver.xpi 添加到 library.zip 存档中?
    • @Eugene 不。据我所知,这是不可能的。如果您找到解决方案,请分享。
    • 我找到了解决方案:您需要修补 firefox_profile.py。在构建之前,将此文件中的 WEBDRIVER_EXT 和 WEBDRIVER_PREFERENCES 更改为 selenium/webdriver/firefox 的相对路径。你会得到类似的东西 ../../../../selenium/webdriver/firefox/webdriver_prefs.json (如果你的答案中的代码被使用)
    【解决方案2】:

    与大多数其他二进制文件一样,可能需要在二进制文件中包含 DLL 或您需要的任何库。例如:

    C:\tests\
      run_tests.exe -- this will read from webdriver.dll
      selenium-webdriver.dll 
    

    此外,从我的 .NET 时代开始,我就知道您实际上能够将库直接嵌入到 EXE 中,这使得它相当大。

    【讨论】:

      【解决方案3】:

      你可以试试pyinstaller,安装简单,使用简单。

      安装:

      pip install pyinstaller
      

      到exe:

      pyinstaller yourprogram.py
      

      【讨论】:

        【解决方案4】:

        这是旧的,但我一直在寻找同样的东西,我确实不得不在许多不同的网站上挖掘我的问题,所以希望这对其他人有帮助。

        我正在使用 py2exe 构建我的 exe 文件,但它不起作用,所以我决定尝试 pyinstaller,是的,它起作用了。

        只是将物品放入物品中以更有条理:

        Py2exe: 我首先从 py2exe 开始,我收到这样的错误: python setup.py py2exe Invalid Syntax (asyncsupport.py, line 22)

        我可以通过删除安装文件中的一些东西来修复它,最后看起来是这样的。

        data_files = [('selenium/webdriver/chrome', ['C:\Python27\Lib\site-packages\selenium\webdriver\chrome\webdriver.py'])]
        
        setup(
            name='General name of app',
            version='1.0',
            description='General description of app',
            author='author name',
            author_email='author email',
            url='',
            windows=[{'script': 'final_headless.py'}],   # the main py file
            data_files=data_files,
            options={
                'py2exe':
                    {
                        'skip_archive': True,
                        'optimize': 2,
                        'excludes': 'jinja2.asyncsupport',
                        'dll_excludes': ["MSVCP90.dll","HID.DLL", "w9xpopen.exe"]
                    }
            }
        )
        

        它可以运行py2exe,但exe文件不起作用,然后我搬到了pyinstaller。

        安装程序: 对我来说,pyinstaller 看起来比 py2exe 容易得多,所以我从现在开始就坚持这一点。我们唯一的“问题”是路径中没有 webdriver,exe 无法运行。 但是,只要将它放在变量路径中,就可以了。

        结论,使用 pyinstaller 是我的解决方案 + 将 webdriver 添加到路径。

        【讨论】:

          猜你喜欢
          • 2010-09-08
          • 2021-12-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-11-20
          • 1970-01-01
          相关资源
          最近更新 更多