前提・実現したいこと
AWSのLambda上でSeleniumやChromedriver, headless-chromiumを用いてGoogle formを毎日指定した時刻に自動入力するコードを開発しています。使用言語はPython 3.7です。
コードの作成やレイヤーへのアップロードはこちらのQiitaのページのやり方にならいました。
ただ、自分の場合はログインが必要なFormを扱うため、
SeleniumでChrome起動→まずGoogleでログイン→その後Formのページへ移動し、自動入力の後ブラウザを閉じる
の流れに変えています。
また、Chromedriverとheadless-chromiumのレイヤーへのアップロードにおいて、上記のページの、
できた二つのzipファイルを解凍し、headless-chromeフォルダにまとめます。その後そのheadless-chromeをzipに圧縮してレイヤーにアップロードします。
この通りに圧縮すると50MBを超えてしまったため、やむなくChromedriverとheadless-chromiumのzipを1つのフォルダにまとめてからそのまとめたフォルダをzipにしてアップロードしました(これで容量が約44MBとなりアップロードできました)。
その後コードを入力し、実行中に以下のエラーメッセージが発生しました。
発生している問題・エラーメッセージ
"errorMessage": "Message: 'chromedriver' executable needs to be in PATH. Please see https://chromedriver.chromium.org/home\n", "errorType": "WebDriverException", "stackTrace": [ " File \"/var/task/lambda_function.py\", line 16, in lambda_handler\n browser = webdriver.Chrome('/opt/headless-chrome/chromedriver',options = options)\n", " File \"/opt/python/selenium/webdriver/chrome/webdriver.py\", line 73, in __init__\n service_log_path, service, keep_alive)\n", " File \"/opt/python/selenium/webdriver/chromium/webdriver.py\", line 90, in __init__\n self.service.start()\n", " File \"/opt/python/selenium/webdriver/common/service.py\", line 83, in start\n os.path.basename(self.path), self.start_error_message)\n" ] }
該当のソースコード・AWSのレイヤー
以下AWS Lambda上でのソースコードです。
※一部今回の質問に無関係な部分は<略>として省略しています。
import json from selenium import webdriver import time import random def lambda_handler(event, context): body_temp = str(36 + random.randint(1,7)/10) url1 = 'https://www.google.com/accounts?hl=ja-JP' options = webdriver.ChromeOptions() options.binary_location = '/opt/headless-chrome/headless-chromium' options.add_argument('--headless') options.add_argument('--no-sandbox') options.add_argument('--single-process') options.add_argument('--disable-dev-shm-usage') browser = webdriver.Chrome('/opt/headless-chrome/chromedriver',options = options) browser.implicitly_wait(1) browser.get(url1) G_ID = browser.find_element_by_id('<略>') G_ID.send_keys('<略>') G_Enter = browser.find_element_by_class_name('<略>') G_Enter.click() G_PW = browser.find_element_by_id('<略>') G_PW.send_keys('<略>') G_Enter.click() time.sleep(3) G_Enter = browser.find_element_by_class_name('<略>') G_Enter.click() time.sleep(2) body_temp = str(36 + random.randint(1,5)/10) url2 = '<Google FormのURL>'+body_temp browser.get(url2) time.sleep(2) try: F_Continue = browser.find_element_by_xpath('<略>') F_Continue.click() except: pass time.sleep(1) Send_Button = browser.find_element_by_xpath('<略>') Send_Button.click() time.sleep(2) browser.close browser.quit return { 'statusCode': 200, 'body': json.dumps('Form submission success!!') }
以下、レイヤー部分のスクリーンショットです。
試したこと
chromedriverのPATHをchromedriverからchromedriver.zipに変更してみました。
browser = webdriver.Chrome('/opt/headless-chrome/chromedriver.zip',options = options)
これでテストしていたところ、また別のエラーが発生してしまいました。
"errorMessage": "[Errno 8] Exec format error: '/opt/headless-chrome/chromedriver.zip'", "errorType": "OSError", "stackTrace": [ " File \"/var/task/lambda_function.py\", line 16, in lambda_handler\n browser = webdriver.Chrome('/opt/headless-chrome/chromedriver.zip',options = options)\n", " File \"/opt/python/selenium/webdriver/chrome/webdriver.py\", line 73, in __init__\n service_log_path, service, keep_alive)\n", " File \"/opt/python/selenium/webdriver/chromium/webdriver.py\", line 90, in __init__\n self.service.start()\n", " File \"/opt/python/selenium/webdriver/common/service.py\", line 76, in start\n creationflags=self.creationflags)\n", " File \"/var/lang/lib/python3.7/subprocess.py\", line 800, in __init__\n restore_signals, start_new_session)\n", " File \"/var/lang/lib/python3.7/subprocess.py\", line 1551, in _execute_child\n raise child_exception_type(errno_num, err_msg, err_filename)\n" ] }
考えられる点としては、やはり/optからのPATHに誤りがあるということなのでしょうか。ディレクトリの名前は間違ってないと思うのですが、それともレイヤーがうまくアップロードできていないのでしょうか。
ほかにも考えられるエラーの原因やコードミスなど、なんでもアドバイスいただけますと幸いです。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。