在AWS Lambda上运行Selenium的问题

时间:2019-05-10 17:06:40

标签: python selenium aws-lambda

我目前正在尝试实施一个刮板,该刮板将每天检查两次,以确定某些PDF是否更改了名称。不幸的是,它需要网站操纵才能找到pdf,因此我认为最好的解决方案是Selenium和AWS Lambda的组合。

首先,我遵循this教程。我已经完成了本教程,但在Lambda中遇到了此错误:

START RequestId: 18637c6d-ea75-40ee-8789-374654700b99 Version: $LATEST
Starting google.com
Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home
: WebDriverException
Traceback (most recent call last):
  File "/var/task/lambda_function.py", line 46, in lambda_handler
    driver = webdriver.Chrome(chrome_options=chrome_options)
  File "/var/task/selenium/webdriver/chrome/webdriver.py", line 68, in __init__
    self.service.start()
  File "/var/task/selenium/webdriver/common/service.py", line 83, in start
    os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home

该错误是其他人所经历的,作者链接到this堆栈溢出页面已得到“解决”。我已经尝试过了,但是所有答案都与在台式机上使用无头铬而不是在AWS Lambda上有关。

我尝试了几次更改都没有用。

1)将chromedriver和headless-chromium更改为.exe文件
2)更改此代码行以包含可执行文件路径

driver = webdriver.Chrome(chrome_options=chrome_options, executable_path=os.getcwd() + "/bin/chromedriver.exe")

在帮助硒和aws lambda协同工作方面的任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:3)

我遇到了同样的问题,这是因为二进制文件位于无法执行它们的位置。添加了移动它们的功能,然后从该位置读取它们,从而对其进行了修复。请参阅下面的示例,该示例是我在研究此错误时刚开始工作的。 (为乱码表示歉意。)

import time
import os
from selenium import webdriver
from fake_useragent import UserAgent

import subprocess
import shutil
import time

BIN_DIR = "/tmp/bin"
CURR_BIN_DIR = os.getcwd() + "/bin"

def _init_bin(executable_name):
    start = time.clock()
    if not os.path.exists(BIN_DIR):
        print("Creating bin folder")
        os.makedirs(BIN_DIR)
    print("Copying binaries for " + executable_name + " in /tmp/bin")
    currfile = os.path.join(CURR_BIN_DIR, executable_name)
    newfile = os.path.join(BIN_DIR, executable_name)
    shutil.copy2(currfile, newfile)
    print("Giving new binaries permissions for lambda")
    os.chmod(newfile, 0o775)
    elapsed = time.clock() - start
    print(executable_name + " ready in " + str(elapsed) + "s.")

def handler(event, context):

    _init_bin("headless-chromium")
    _init_bin("chromedriver")

    chrome_options = webdriver.ChromeOptions()

    chrome_options.add_argument('--headless')
    chrome_options.add_argument('--disable-gpu')
    chrome_options.add_argument('--window-size=1280x1696')
    chrome_options.add_argument('--no-sandbox')
    chrome_options.add_argument('--hide-scrollbars')
    chrome_options.add_argument('--enable-logging')
    chrome_options.add_argument('--log-level=0')
    chrome_options.add_argument('--v=99')
    chrome_options.add_argument('--single-process')
    chrome_options.add_argument('--ignore-certificate-errors')

    chrome_options.binary_location = "/tmp/bin/headless-chromium"
    driver = webdriver.Chrome("/tmp/bin/chromedriver", chrome_options=chrome_options)
    driver.get('https://en.wikipedia.org/wiki/Special:Random')
    line = driver.find_element_by_class_name('firstHeading').text
    print(line)
    driver.quit()

    return line

答案 1 :(得分:1)

我也遇到了同样的问题,但现在已经修复。在我的情况下,lambda和My Dockerfile上的python版本并不相同。