Running pyinstaller another pc with Chromedriver

前端 未结 1 586
情歌与酒
情歌与酒 2020-12-05 16:57

I am trying to add Chromedriver inside an executable in pyinstaller. While this is possible it seems that I get the below error message when trying to run this on another c

相关标签:
1条回答
  • 2020-12-05 17:11

    Use --add-binary to bundle the driver in the application:

    pyinstaller -F --add-binary "C:\drivers\chromedriver.exe";"." script.py
    

    and use sys._MEIPASS to get the folder where the driver is extracted:

    import sys, os, time
    from selenium import webdriver
    
    if __name__ == "__main__":
    
      if getattr(sys, 'frozen', False): 
        # executed as a bundled exe, the driver is in the extracted folder
        chromedriver_path = os.path.join(sys._MEIPASS, "chromedriver.exe")
        driver = webdriver.Chrome(chromedriver_path)
      else:
        # executed as a simple script, the driver should be in `PATH`
        driver = webdriver.Chrome()
    
      driver.get("https://stackoverflow.com")
      time.sleep(5)
    
      driver.quit()
    
    0 讨论(0)
提交回复
热议问题