运行脚本而不下载chromedriver

时间:2019-03-25 14:51:17

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

我想知道在另一台计算机上运行Python脚本的最简单方法。 我可以通过PyInstaller和cx_Freeze在exe中编译脚本。 如何在不更改脚本路径(driver = webdriver.Chrome('C:\py\chromedriver'))到ChromeDriver的情况下完成此操作?

1 个答案:

答案 0 :(得分:1)

通常,我们将所有必需的驱动程序放置在项目中,然后在项目中提供驱动程序的路径。像这样的东西

project
   drivers
   tests
   any other folders

现在您可以访问将所有驱动程序放置在drivers文件夹中,并按以下方式进行访问。

def get_full_path_to_folder(folderName):
    folders = os.path.abspath(os.pardir).split(os.sep)
    folderPath = ''
    for folder in folders:
        if folderPath == '':
            folderPath = folder
        else:
            folderPath = folderPath + "\\" +folder
        if os.path.isdir(os.path.join(folderPath, folderName)):
            return os.path.join(folderPath, folderName)
            break

driver = webdriver.Chrome(executable_path=os.path.join(get_full_path_to_folder('drivers'), "chromedriver.exe"))

通过这种方式,驱动程序将与您计划的.exe一起使用。

相关问题