在AWS Lambda上使用Python 3.6运行Headless Chrome-权限错误 [英] Running Headless Chrome using Python 3.6 on AWS Lambda - Permissions Error

查看:68
本文介绍了在AWS Lambda上使用Python 3.6运行Headless Chrome-权限错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力让无头Chrome在AWS Lambda上运行几天.它在EC2上可以正常工作,但是当我在Lambda上尝试时,我只是得到消息:'chromedriver'可执行文件可能具有错误的权限.

I have struggled to get Headless Chrome running on AWS Lambda for days. It works fine on EC2 but when I try it on Lambda, I just get "Message: 'chromedriver' executable may have wrong permissions.

在zip文件的根目录中,使用chromedriver和headless-chromium可执行文件压缩模块.我上传到S3的压缩文件总数为52mb,但提取的文件数低于250mb的限制,因此我认为这不是问题.

The modules are zipped with the chromedriver and headless-chromium executables in the root directory of the zip file. The total zipped file I upload to S3 is 52mb but extracted it is below the 250mb limit so I don't think that is the issue.

Python Zip文件夹结构图像

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和其他方面取得了成功.

A few posts on the web have reported compatibility issues that may have caused problems so I have the specific executable versions for Chrome and ChromeDriver from the web, where others seem to on previous success EC2 and other means.

无头铬钢和铬木矿工的下载资源(稳定) 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/

DOWNLOAD SOURCES FOR HEADLESS CHROME AND CHROMEDRIVER (stable) https://github.com/adieuadieu/serverless-chrome/releases/tag/v1.0.0-37 (https://sites.google.com/a/chromium.org/chromedriver/downloads) Download unavailable so retrieved from the source below https://chromedriver.storage.googleapis.com/index.html?path=2.37/

有人可以帮我破解这个问题吗?

Can anyone help me crack this?

推荐答案

几分钟前,我找到了解决此问题的方法.在Lambda Function中使用chromedriver时(我认为),需要写权限即可.但是当chrome驱动程序文件位于任务"文件夹或选择"文件夹中时,用户只能拥有读取权限.

I found a solution for this problem few minutes ago. When use chromedriver in Lambda Function (I think) it need permission can write. but when chrome driver file is in 'task' folder or 'opt' folder, user can only have read permission.

在Lambda函数中,只有文件夹可以更改权限.功能是'tmp'文件夹.

Only folder can change permission in Lambda Function is 'tmp' folder.

因此,我将chrome驱动程序文件移至"tmp"文件夹.它的工作原理.

So I move the chrome driver file to 'tmp' folder. and it works.

像这样

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)

这篇关于在AWS Lambda上使用Python 3.6运行Headless Chrome-权限错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆