2017-11-06 7 views
0

私は、Seleniumを使用して一部のWebサイトからデータを取得するPythonプログラムを作成しました。いくつかの入力引数と(1)テキストファイル(2)Firefoxのバイナリ&(3)ヤモリドライバ今Pythonコードは動作しますが、py2exeを使って実行できません。

私はpy2exeを使用して実行可能ファイルにこれを変換している私の同僚でこれを共有する:これは私のコードは、3つのファイルを必要としていることを意味します。

シェルから実行された場合のプログラムは一切動作しません。 (2)geckoドライバが見つかりませんでした。(3)Windowsがエラー6を処理しました(毎回これらのエラーがすべて発生するわけではありません)。 ) それが実行可能ファイルとしても動作するようにする方法を教えてください。コードから

抜粋:

gdir = str(os.path.realpath('.')) 
gdir = gdir.replace('\\','\\\\') 
inpArgPath = gdir + '\\\\inputArguments.txt' 

ffpath = 'FirefoxPortable/App/Firefox64/firefox.exe' 
binary = webdriver.firefox.firefox_binary.FirefoxBinary(ffpath) 
gecko = gdir + '\\\\gecko\\\\geckodriver64.exe' 

fp = webdriver.FirefoxProfile() 
fp.set_preference("browser.download.folderList",2) 
fp.set_preference("browser.download.manager.showWhenStarting",False) 
fp.set_preference("browser.download.forbid_open_with",True) 
fp.set_preference("browser.download.altClickSave",True) 
fp.set_preference("browser.download.manager.showWhenStarting",False) 

browser = webdriver.Firefox(firefox_binary=binary, firefox_profile=fp, executable_path=gecko) 

セレンエラー:エラーの

Traceback (most recent call last): 
    File "weeklyData_v5.py", line 18, in <module> 
IOError: [Errno 2] No such file or directory: 'inputArguments.txt' 
Traceback (most recent call last): 
    File "weeklyData_v5.py", line 18, in <module> 
IOError: [Errno 2] No such file or directory: 'inputArguments.txt' 

つ以上の実施例:

File "selenium\webdriver\firefox\webdriver.pyc", line 144, in __init__ 
    File "selenium\webdriver\common\service.pyc", line 81, in start 
selenium.common.exceptions.WebDriverException: Message: 'geckodriver32.exe' executable needs to be in PATH. 

-

browser = webdriver.Firefox(firefox_binary=binary, firefox_profile=fp, executable_path=gecko) 
    File "selenium\webdriver\firefox\webdriver.pyc", line 144, in __init__ 
    File "selenium\webdriver\common\service.pyc", line 74, in start 
    File "subprocess.pyc", line 382, in __init__ 

    File "subprocess.pyc", line 515, in _get_handles 

    File "subprocess.pyc", line 566, in _make_inheritable 

WindowsError: [Error 6] The handle is invalid 

答えて

0

あなたの間違いはそれを考えることです。あなたのスクリプトのパスです。そうではありません。これは現在の作業ディレクトリに対する相対パスです。これは、実行可能ファイルが入っているディレクトリである可能性があります。しかし、それはまったく異なるところにある可能性があります。

実行可能ファイルであるプログラムに与えられた最初のコマンドライン引数を代わりに使用する必要があります。

import os 
import sys 
BASE = os.path.dirname(sys.argv[0]) 
+0

ありがとうございます。私はこの変更を加えたが、コードはまだ動作する。しかし、実行可能ファイル(py2exe)を実行すると、 "NameError:name '__ file __'が定義されていません"というエラーメッセージが表示されます。 – dsauce

+0

@dsauce更新された回答を見ると、py2exeの専門があります。 – deets

+0

ありがとうございます。これによりコードが(exeとして)動作することがありましたが、時にはエラーが発生することがあります(例:geckoドライバが見つかりません)、または「WindowsError:[Error 6]ハンドルが無効です」。質問テキストをより多くのエラーの例で更新しました。 – dsauce

関連する問題