Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在AWS Lambda上运行Selenium的问题_Python_Selenium_Aws Lambda - Fatal编程技术网

Python 在AWS Lambda上运行Selenium的问题

Python 在AWS Lambda上运行Selenium的问题,python,selenium,aws-lambda,Python,Selenium,Aws Lambda,我目前正在尝试实现一个scraper,它将每天检查两次特定PDF是否更改名称。不幸的是,它需要网站操作来找到PDF,所以我认为最好的解决方案是将Selenium和AWS Lambda结合起来 首先,我遵循教程。我已经完成了教程,但在Lambda中遇到了以下错误: START RequestId: 18637c6d-ea75-40ee-8789-374654700b99 Version: $LATEST Starting google.com Message: 'chromedriver' exe

我目前正在尝试实现一个scraper,它将每天检查两次特定PDF是否更改名称。不幸的是,它需要网站操作来找到PDF,所以我认为最好的解决方案是将Selenium和AWS Lambda结合起来

首先,我遵循教程。我已经完成了教程,但在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
其他人也遇到过此错误,作者通过链接到堆栈溢出页面“解决”了此错误。我已经试过了,但所有的答案都是关于在桌面上使用无头铬,而不是AWS lambda

我做了几次改变,但都没用

1) 将chromedriver和headless Chrome更改为.exe文件
2) 更改这行代码以包括可执行路径

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

如果您能帮助selenium和aws lambda一起工作,我们将不胜感激。

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


我也有同样的问题,但我现在已经解决了。在我的例子中,lambda和我的Dockerfile上的python版本不一样。

是否已将下载的Chrome文件添加为部署包的一部分,如果是,请尝试将驱动程序命令的路径更改为以下内容:
driver=webdriver.Chrome(Chrome\u options=Chrome\u options,executable\u path=os.getcwd()+“/chromedriver.exe”)
很抱歉响应太晚,但我尝试使用“/”并仍收到相同的错误我正在尝试您的解决方案,但仍收到“消息:无法连接到服务/tmp/bin/chromedriver”“来自selenium的错误。你以前有没有遇到过这个问题?你使用的是什么版本的selenium、chromedriver和headless chromium?我不记得那个错误了。我使用的是二进制和chrome驱动程序,如果你克隆并下载的话,我意识到我没有改变我的PYTHONPATH,我想这解决了我的问题。我遵循了你的解决方案。然而,我得到了这个
“erromessage”:“[Errno 2]没有这样的文件或目录:'/var/task/bin/headless chromium'”,
之后。你知道原因吗?我在AWS lambda上运行它,似乎bin dir没有我试图复制到的可执行文件
/tmp
,是的,它确实是这样。我强烈建议您使用AWS SAM,通过docker进入桌面上类似lambda的环境。它将允许您四处查看,确保您拥有权限,并且文件确实存在。也使部署更加容易。
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