Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
426 views
in Technique[技术] by (71.8m points)

python - Running pyinstaller another pc with 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.

Code 1:

Main.py

from selenium import webdriver
driver = webdriver.Chrome()

What I get when running on another pc:

Error 1:

Cannot find Chrome Path

   C:UsersAperture ScienceDesktop1>123.exe
    Traceback (most recent call last):
      File "site-packagesseleniumwebdrivercommonservice.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-packagesseleniumwebdriverchromewebdriver.py", line 68, in __init__
      File "site-packagesseleniumwebdrivercommonservice.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

How can I get around this?

What I get from link provided:

Code 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/")

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

pyinstaller -F --add-binary "C:driverschromedriver.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()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...