実現したいこと
AWS Lambda (python)でseleniumを使ってwebスクレイピングを行いたいが、
Lambda実行時にエラー発生するため、エラーを解消したい
問題点
まずは find_element_by_name で要素の取得を行えるかを確認しようとしたところ、
下記のコードをLambdaに書いてテスト実行するとエラーが発生する。
- ソースコード
# python配下自動でimport from selenium import webdriver from selenium.webdriver.support.select import Select def lambda_handler(event, context): # webサイトにアクセス URL = "https://www.net.city.nagoya.jp/cgi-bin/sp05001" options = webdriver.ChromeOptions() options.add_argument("--headless") options.add_argument("--disable-gpu") options.add_argument("--hide-scrollbars") options.add_argument("--single-process") options.add_argument("--ignore-certificate-errors") options.add_argument("--window-size=880x996") options.add_argument("--no-sandbox") options.add_argument("--homedir=/tmp") options.binary_location = "/opt/headless/python/bin/headless-chromium" #ブラウザの定義 browser = webdriver.Chrome( "/opt/headless/python/bin/chromedriver", options=options ) browser.get(URL) title = browser.title syumoku = browser.find_element_by_name('syumoku') # 種目の要素を取得 Select(syumoku).select_by_index(23) # バスケットボールを選択 browser.close() return
- エラーメッセージ
{ "errorMessage": "Message: no such element: Unable to locate element: {\"method\":\"css selector\",\"selector\":\"[name=\"syumoku\"]\"}\n (Session info: headless chrome=64.0.3282.167)\n (Driver info: chromedriver=2.37.544315 (730aa6a5fdba159ac9f4c1e8cbc59bf1b5ce12b7),platform=Linux 4.14.255-273-220.498.amzn2.x86_64 x86_64)\n", "errorType": "NoSuchElementException", "stackTrace": [ " File \"/var/task/lambda_function.py\", line 30, in lambda_handler\n syumoku = browser.find_element_by_name('syumoku') # 種目の要素を取得\n", " File \"/opt/python/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py\", line 665, in find_element_by_name\n return self.find_element(by=By.NAME, value=name)\n", " File \"/opt/python/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py\", line 1250, in find_element\n 'value': value})['value']\n", " File \"/opt/python/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py\", line 425, in execute\n self.error_handler.check_response(response)\n", " File \"/opt/python/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py\", line 247, in check_response\n raise exception_class(message, screen, stacktrace)\n" ] }
nameタグの'syumoku'がないというエラーだと思いますが、
今回指定したwebサイトを確認するとnameタグの'syumoku'は確認できる。
何が問題でエラーなのかが分かりません。
事前に確認したこと
https://qiita.com/ichihara-development/items/5e61c3424b3176bc6096
のページを参考にさせて頂き、サンプルコードでの動作確認は実施済です。
特にエラーは発生しなかったので、selenium、chromedriver、headless-chromium
のインストールやLambdaレイヤーの作成などは問題ないと思っています。
- サンプルコード
# python配下自動でimport from selenium import webdriver def lambda_handler(event, context): URL = "https://news.yahoo.co.jp/" options = webdriver.ChromeOptions() options.add_argument("--headless") options.add_argument("--disable-gpu") options.add_argument("--hide-scrollbars") options.add_argument("--single-process") options.add_argument("--ignore-certificate-errors") options.add_argument("--window-size=880x996") options.add_argument("--no-sandbox") options.add_argument("--homedir=/tmp") options.binary_location = "/opt/headless/python/bin/headless-chromium" #ブラウザの定義 browser = webdriver.Chrome( "/opt/headless/python/bin/chromedriver", options=options ) browser.get(URL) title = browser.title browser.close() return title
- ログ貼り付け
実行結果: 成功(ログ) 詳細 関数の実行から返された結果が以下のエリアに表示されます。関数から結果を返す方法の詳細については、こちらをご参照ください。 "Yahoo!ニュース" 概要 コード SHA-256 c58SQmsfJ1iD9xs4GXsFXLg1ZOp6Pkf6QvmHWvW98d4= リクエスト ID 07ad0c56-b30b-4efe-a6d8-01f36f4a72cc 初期所要時間 243.01 ms 所要時間 37484.14 ms 課金期間 37485 ms 設定済みリソース 256 MB 使用中の最大メモリ 256 MB ログ出力 以下のセクションでは、コード内のロギング呼び出しを示しています。ここをクリックし、 で対応する CloudWatch ロググループを表示できます。 START RequestId: 07ad0c56-b30b-4efe-a6d8-01f36f4a72cc Version: $LATEST END RequestId: 07ad0c56-b30b-4efe-a6d8-01f36f4a72cc REPORT RequestId: 07ad0c56-b30b-4efe-a6d8-01f36f4a72cc Duration: 37484.14 ms Billed Duration: 37485 ms Memory Size: 256 MB Max Memory Used: 256 MB Init Duration: 243.01 ms

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/04/14 20:13
2022/04/15 00:53
2022/04/15 05:50 編集
2022/04/17 14:47
2022/04/18 01:06 編集