【问题标题】:python - path to chromedriver.exe (running on server)python - chromedriver.exe 的路径(在服务器上运行)
【发布时间】:2021-08-31 17:51:36
【问题描述】:

我曾经在我的电脑上运行一个程序,一切正常.. 一旦我开始使用 Flask 和 Thread 在服务器上运行它,我就无法上传 chrome 驱动程序文件

driver = webdriver.Chrome(executable_path = "chromedriver.exe", options = options)

该文件位于同一目录中,可能是因为它在服务器上运行,我需要不同的路径。 哪个文件路径是正确的?

【问题讨论】:

    标签: python selenium server


    【解决方案1】:

    不,您不需要 chromedriver.exe 那样传递它。在这种情况下,您可以使用自动安装程序

    只需这样做:-

    pip install chromedriver-autoinstaller
    

    然后像这样初始化浏览器:

    from selenium import webdriver
    import chromedriver_autoinstaller
    
    
    chromedriver_autoinstaller.install()  # Check if the current version of chromedriver exists
                                          # and if it doesn't exist, download it automatically,
                                          # then add chromedriver to path
    
    driver = webdriver.Chrome()
    driver.get("http://www.python.org")
    assert "Python" in driver.title
    

    如果你注意,我们没有硬编码路径

    driver = webdriver.Chrome(),并且每次运行脚本时它仍然能够获取最新的 chromedriver。基本上说再见chromedriver.exe 和升级它的繁琐工作,每当我们有一个新的铬构建。

    【讨论】:

    • 我们如何设置 driver = webdriver.Chrome (options = options)
    • 您需要为此定义 chromeoptions。看到一切都一样,它只是一个自动安装程序。所以从webdriver.Chrome(options = options)中删除chromedriver.exe
    猜你喜欢
    • 1970-01-01
    • 2017-02-21
    • 2017-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-16
    • 2021-05-27
    相关资源
    最近更新 更多