【问题标题】:Program using selenium fails after building with cx_freeze使用 cx_freeze 构建后使用 selenium 的程序失败
【发布时间】:2013-11-30 00:33:20
【问题描述】:

我正在使用 Selenium (v2.37.2) 开发一个自动网络测试器。程序正常运行,直到我运行使用 cxfreeze 构建的测试(还有 tkinter gui)。

有初始化函数

def initDriver(self):

    if self.browser == FIREFOX:
        profile = webdriver.FirefoxProfile(profile_directory=self.profile);
        self._driver = webdriver.Firefox(firefox_profile=profile)

    elif self.browser == CHROME:            
        self._driver = webdriver.Chrome(self.executable, chrome_options=profile)

    elif self.browser == IEXPLORER:
        self._driver = webdriver.Ie(self.executable)

现在当我使用 Cx_freeze 构建它时,我得到了这个错误

方法redirectToBlank(...) 调用initDriver(..) 作为第一件事那么我如何将.xpi 文件打包到library.zip 文件中- 我必须使用setup.py 中的哪个选项?我什至必须这样做吗?

第二个奇怪的事情是,当我通过单击其图标执行 .exe 文件时,其他浏览器工作正常,但是当我从命令行运行它时,即使对于 chrome 和 IE,我也会遇到错误。 (抱歉,回溯不完整)

所有路径都是相对于执行文件的(无论你从哪里运行它),

感谢您提出解决此问题的任何想法。

(方法redirectToBlank(...) 调用initDriver(..) 作为第一件事)

【问题讨论】:

    标签: selenium python-3.x webdriver selenium-webdriver cx-freeze


    【解决方案1】:

    第一个问题解决了 这是 selenium - FirefoxProfile - 类的问题,它试图将 webdriver.xpi 作为普通文件加载,但是 selenium 将所有库打包到一个 zip 文件中,所以 selenium 找不到它。 即使在安装文件中强制 cx_freeze 将 webdriver.xpi 添加到 zip 中的正确目录也无济于事。

    有必要像这样编辑 FirefoxProfile(在 firefox_profile 模块中)类

    def _install_extension(self, addon, unpack=True):
        """
            Installs addon from a filepath, url
            or directory of addons in the profile.
            - path: url, path to .xpi, or directory of addons
            - unpack: whether to unpack unless specified otherwise in the install.rdf
        """
        if addon == WEBDRIVER_EXT:
            # altered lines
            import sdi.env
            WEBDRIVER_SUBSTITUTE = "path/to/unpacked/webdrive.xpi"
            addon = os.path.join(os.path.dirname(__file__),  WEBDRIVER_SUBSTITUTE)
    
            # Original lines:
            # addon = os.path.join(os.path.dirname(__file__),  WEBDRIVER_EXT)
    
        < the rest of the method >
    

    问题 2

    OSError: win error 6: the handle is invalid 问题不是由 cxfreeze 或 selenium 引起的。我从 git bash 运行最终的 exe 文件。有问题。由于某种原因 git bash 没有为程序打开标准输入,这就是它失败的原因。当我在标准 Windows 命令行中运行它时,一切正常,或者如果我从 git bash 运行它,例如 program.exe &lt; empty_file

    【讨论】:

    • 实际上,这是一个 Selenium 问题,我也遇到了同样的问题(我也在使用 Selenium 和 Qthread + cx_freeze)。第二个问题可以在 webdriver/common/service.py 中解决,方法是在发生 subprocess.Open() 的第 72 行周围定义标准输入。尝试通过以下方式定义标准输入:stdin=subprocess.PIPE
    【解决方案2】:

    我所做的是删除 selenium 表单包列表。 并将其放入includefiles,然后它就可以工作了。

    像这样:

    includefiles = [(seleniumPackage,'')]
    
    ...
    options = {'build_exe': {'includes':includes,
                                 'excludes':excludes,
                                 'optimize':2,
                                 'packages':packages,
                                 'include_files':includefiles,
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-19
      • 2018-10-26
      • 2021-10-18
      • 1970-01-01
      • 2020-03-16
      • 1970-01-01
      • 2013-05-23
      • 1970-01-01
      相关资源
      最近更新 更多