使用Chromedriver在另一台PC上运行pyinstaller [英] Running pyinstaller another pc with Chromedriver

查看:355
本文介绍了使用Chromedriver在另一台PC上运行pyinstaller的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在pyinstaller的可执行文件中添加Chromedriver.虽然这是可能的,但似乎在尝试在另一台计算机上运行此错误消息.

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 computer.

我尝试了许多帖子,包括

I have tried a number of posts including this one, but unfortunately, this has not provided desired results. Best case was I could run it on my own computer when chrome exe was in the same folder which was unhelpful.

代码1:

Main.py

from selenium import webdriver
driver = webdriver.Chrome()

在另一台PC上运行时得到的信息:

What I get when running on another pc:

错误1:

找不到Chrome路径

Cannot find Chrome Path

   C:\Users\Aperture Science\Desktop\1>123.exe
    Traceback (most recent call last):
      File "site-packages\selenium\webdriver\common\service.py", line 74, in start
      File "subprocess.py", line 709, in __init__
      File "subprocess.py", line 997, in _execute_child
    FileNotFoundError: [WinError 2] The system cannot find the file specified

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
      File "main.py", line 42, in <module>
      File "main.py", line 33, in main
      File "site-packages\selenium\webdriver\chrome\webdriver.py", line 68, in __init__
      File "site-packages\selenium\webdriver\common\service.py", line 81, in start
    selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home

    [2228] Failed to execute script main

我该如何解决?

我从提供的链接中得到什么:

What I get from link provided:

代码2:

from selenium import webdriver
import os, sys, inspect
current_folder = os.path.realpath(os.path.abspath(os.path.split(inspect.getfile(inspect.currentframe() ))[0]))
chromedriver = os.path.join(current_folder,"chromedriver.exe")
driver = webdriver.Chrome(executable_path = chromedriver)
driver.get("http://www.imdb.com/")

需要设置路径中的Chrome exe,捆绑的Chrome无法读取.因此,打包的chrome无法正常工作.

REQUIRES Chrome exe in set path, bundled chrome not read. So packaged chrome does not work as desired.

推荐答案

使用--add-binary将驱动程序捆绑在应用程序中:

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

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

并使用sys._MEIPASS获取提取驱动程序的文件夹:

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()

这篇关于使用Chromedriver在另一台PC上运行pyinstaller的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆