【问题标题】:Running Headless Chrome using Python 3.6 on AWS Lambda - Permissions Error在 AWS Lambda 上使用 Python 3.6 运行 Headless Chrome - 权限错误
【发布时间】:2018-08-27 21:12:20
【问题描述】:

几天来,我一直在努力让 Headless Chrome 在 AWS Lambda 上运行。它在 EC2 上运行良好,但是当我在 Lambda 上尝试时,我得到了"Message: 'chromedriver' executable may have wrong permissions.

模块使用 chromedriver 和 headless-chromium 可执行文件在 zip 文件的根目录中进行压缩。我上传到 S3 的总压缩文件为 52mb,但解压后低于 250mb 限制,所以我认为这不是问题。

Python Zip Folder Structure Image

from selenium import webdriver


def lambda_handler(event, context):
    options = webdriver.ChromeOptions()
    options.add_argument("--headless")
    options.add_argument("--disable-gpu")
    options.add_argument("--window-size=1280x1696")
    options.add_argument("--disable-application-cache")
    options.add_argument("--disable-infobars")
    options.add_argument("--no-sandbox")
    options.add_argument("--hide-scrollbars")
    options.add_argument("--enable-logging")
    options.add_argument("--log-level=0")
    options.add_argument("--v=99")
    options.add_argument("--single-process")
    options.add_argument("--ignore-certificate-errors")
    options.add_argument("--homedir=/tmp")
    options.binary_location = "/var/task/headless-chromium"

    driver = webdriver.Chrome("/var/task/chromedriver", chrome_options=options)
    driver.get("https://www.google.co.uk")
    title = driver.title
    driver.close()

    return title


if __name__ == "__main__":
    title = lambda_handler(None, None)
    print("title:", title)

网络上的一些帖子报告了可能导致问题的兼容性问题,因此我从网络上获得了 Chrome 和 ChromeDriver 的特定可执行版本,其他人似乎在以前的成功 EC2 和其他方式上。

下载 HEADLESS Chrome 和 CHROMEDRIVER 的资源 (稳定)https://github.com/adieuadieu/serverless-chrome/releases/tag/v1.0.0-37 (https://sites.google.com/a/chromium.org/chromedriver/downloads) 下载不可用,所以从以下来源检索 https://chromedriver.storage.googleapis.com/index.html?path=2.37/

谁能帮我破解这个?

【问题讨论】:

  • 将两个可执行文件(chromedriver 和 headless-chromium)的权限更改为 777 并上传。问题已解决。
  • 你解决了这个问题吗?我正在使用 Windows 使用 SAM 构建在本地运行我的 lambda,并且它运行良好。我的权限似乎也正确。真的很感激
  • @SMDC 你应该从 Linux 部署文件,如果你有 Win 10,WSL 也可以工作。

标签: python-3.x selenium aws-lambda selenium-chromedriver google-chrome-headless


【解决方案1】:

几分钟前我找到了解决这个问题的方法。 在 Lambda 函数(我认为)中使用 chromedriver 时,它需要权限才能写入。但是当 chrome 驱动程序文件在“task”文件夹或“opt”文件夹中时,用户只能拥有读取权限。

只有文件夹可以更改 Lambda 函数中的权限是 'tmp' 文件夹。

所以我将 chrome 驱动程序文件移动到“tmp”文件夹。它有效。

像这样。

os.system("cp ./chromedriver /tmp/chromedriver")
os.system("cp ./headless-chromium /tmp/headless-chromium")
os.chmod("/tmp/chromedriver", 0o777)
os.chmod("/tmp/headless-chromium", 0o777)
chrome_options.binary_location = "/tmp/headless-chromium"
driver = webdriver.Chrome(executable_path=r"/tmp/chromedriver",chrome_options=chrome_options)

【讨论】:

    猜你喜欢
    • 2023-02-20
    • 1970-01-01
    • 2021-04-02
    • 2020-08-20
    • 2017-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-25
    相关资源
    最近更新 更多