我尝试在 AWS Lambda 上使用 Firefox WebDriver 和 Selenium,使用三个 lambda 层:
这是我的代码:
import json
from shutil import copyfile
import os
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
def webdriver_permissions(origin_path, destiny_path):
''' The geckodriver and firefox binaries are set in the lambda
function through a lambda layer. However, by default these files are saved
in '/opt' folder and hence they are not executable. This function copy the
binaries to '/tmp' folder and grant them with executable permissions '''
copyfile(origin_path, destiny_path)
os.chmod(destiny_path, 0o775)
def lambda_handler(event, context):
webdriver_permissions('/opt/geckodriver','/tmp/geckodriver')
webdriver_permissions('/opt/firefox/firefox','/tmp/firefox')
options = Options()
options.headless = True
options.binary = FirefoxBinary('/tmp/firefox')
driver = webdriver.Firefox(
options=options,
executable_path="/tmp/geckodriver",
log_path="/tmp/geckodriver.log"
)
url = 'foo'
driver.get(url)
当我运行代码时,代码的 geckodriver 部分工作正常,但是当我到达二进制选项时,会出现此错误:
"errorMessage": "Message: binary is not a Firefox executable\n"
我几乎尝试了一切:
/tmp
对此有什么建议吗?
将文件改为 zip,而不是 tar