在没有安装铬的情况下启动基于硒的独立式exe

时间:2017-08-07 20:46:02

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

我创建了一个使用selenium和webdriver(Chromedriver)的python脚本。完成此脚本后,我使用cx_freeze将我的脚本编译成一个独立的exe文件,我可以双击它来执行脚本。但是,使用selenium,我一直在使用我下载的chromedriver文件,它可以在我安装到我的电脑上的chrome应用程序中运行。

我想做的或尝试做的是让我的exe文件与chromedriver一起使用,而不要求用户将google chrome安装到他们的计算机上。无论如何,我可以将chrome作为一个包包含在同一个目录中以解决这个问题吗?

我也对其他想法持开放态度。

1 个答案:

答案 0 :(得分:0)

您始终可以捆绑脱机安装程序并提示安装它,然后运行可执行文件进行安装。 (可用here

注意:您可能需要考虑一些许可问题。

否则,请使用webbrowser.open('https://www.google.com/chrome/browser/')将其定向到Chrome的安装页面。类似的东西:

try:
    selenium.webbrowser('chrome')  # Or whatever the command is
except NameOfExceptionWhenNoBrowserHere:
    # If you are opening the web page:

    import webbrowser

    INSTALL_PAGE = 'https://www.google.com/chrome/browser/'
    print('You need to have Google chrome installed.')
    print(INSTALL_PAGE)
    if input('Open ' + INSTALL_PAGE + '? [yes/no]').lower().startswith('y'):
        webbrowser.open(INSTALL_PAGE)  # Uses default browser

    # If you are running the offline installer

    import subprocess
    import os

    _file_dir = os.path.dirname(os.path.realpath(__file__))

    print('You need to have Google chrome installed.')
    print('Will now open installer')

    # _file_dir is the directory of the python file.
    # If the installer is in the same directory, run it like this.
    subprocess.call(os.path.join(_file_dir, 'install_chrome.exe'))