从python selenium测试生成exe文件

从python selenium测试生成exe文件,python,selenium,pyinstaller,Python,Selenium,Pyinstaller,我尝试在exe文件中构建python selenium测试,并在许多机器上运行它,以保持测试独立于环境。但result*.exe文件找不到selenium webdriver。如何在*.exe文件中包含所有selenium依赖项?或者有没有别的办法? 是否可以创建并分发虚拟环境?与大多数其他二进制文件一样,可能需要在二进制文件中包含DLL或您需要的任何库。例如: C:\tests\ run_tests.exe -- this will read from webdriver.dll se

我尝试在exe文件中构建python selenium测试,并在许多机器上运行它,以保持测试独立于环境。但result*.exe文件找不到selenium webdriver。如何在*.exe文件中包含所有selenium依赖项?或者有没有别的办法?
是否可以创建并分发虚拟环境?

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

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

另外,从.NET时代起,我就知道您能够将库直接嵌入EXE,这使得它相当大。

我假设您正在使用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,
            }
    }
)
您可以尝试,它安装简单,使用简单

安装:

pip install pyinstaller
要执行:

pyinstaller yourprogram.py

这是旧的,但我一直在寻找相同的东西,我必须在许多不同的网站挖掘,以找出我的问题,所以希望这将有助于其他人

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

只是要把东西放在项目中以便更有条理:

Py2exe: 我首先从py2exe开始,遇到如下错误:

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

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: 对我来说,pyinstaller看起来比py2exe简单得多,所以我从现在开始就坚持使用它。我们唯一的“问题”是,如果路径中没有webdriver,exe将无法运行。 但一旦你有了它,你就可以走了


结论:使用pyinstaller是我的解决方案+将webdriver添加到path。

谢谢,它可以工作,但我的文件太多了。是否可以将webdriver_prefs.json和webdriver.xpi添加到library.zip存档中?@Eugene否。据我所知,这是不可能的。如果您找到了解决方案,请与我们分享。我找到了解决方案:您需要修补firefox_profile.py。在构建之前,将此文件中的WEBDRIVER_EXT和WEBDRIVER_首选项更改为selenium/WEBDRIVER/firefox的相对路径。您将得到这样的结果../../../../../selenium/webdriver/firefox/webdriver\u prefs.json(如果您的答案中使用了代码)