首页 > 解决方案 > 使用 python 在单个 Cloudwatch Synthetics Canary 中检查多个 URL

问题描述

我想检查单个 CloudWatch Synthetics Canary 中的多个 (100+) URL。我从这篇博文开始我的脚本: https ://aws.amazon.com/blogs/mt/create-canaries-in-python-and-selenium-using-amazon-cloudwatch-synthetics/

但是经过一些小的修改,我得到了以下错误:

TypeError: 'NoneType' object is not callable
ERROR: Canary execution exception.Traceback (most recent call last):
File "/var/task/index.py", line 74, in handle_canary response = await customer_canary.handler(event, context) 
File "/opt/python/bm_check.py", line 36, in handler return await main() File "/opt/python/bm_check.py", line 28, in main await webdriver.execute_step("Navigate to home", navigate_to_home(url)) 
File "/opt/python/aws_synthetics/core/base_synthetics.py", line 231, in execute_step return_value = function_to_execute()

我的金丝雀脚本:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import selenium.common.exceptions
from aws_synthetics.selenium import synthetics_webdriver as webdriver
from aws_synthetics.common import synthetics_logger as logger
from aws_synthetics.common import synthetics_configuration

TIMEOUT = 5


async def main():
    browser = webdriver.Chrome()

    synthetics_configuration.set_config(
        {
            "screenshot_on_step_start": False,
            "screenshot_on_step_success": True,
            "screenshot_on_step_failure": True
        }
    )
    
    def navigate_to_home(url):
        browser.implicitly_wait(TIMEOUT)
        browser.get(url)
    
    url = "https://d2h3ljlsmzojxz.cloudfront.net/"
    await webdriver.execute_step("Navigate to home", navigate_to_home(url))

    url = "youtube.com"
    await webdriver.execute_step("Navigate to home", navigate_to_home(url))
    
    logger.info("---------Finished the execution---------")
    
async def handler(event, context):
    return await main()

我的最终目标是使用 for 循环检查这些 URL,并将每个 URL 视为一个执行步骤,对成功和失败进行截图。我究竟做错了什么?

提前致谢!

编辑:我咨询了一个 aws 合作伙伴并帮助我找到了解决方案。在 url 中必须使用 http(s) 协议。我的工作脚本:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import selenium.common.exceptions
from aws_synthetics.selenium import synthetics_webdriver as webdriver
from aws_synthetics.common import synthetics_logger as logger
from aws_synthetics.common import synthetics_configuration

TIMEOUT = 5


async def main():
    browser = webdriver.Chrome()

    synthetics_configuration.set_config(
        {
        "screenshot_on_step_start": False,
        "screenshot_on_step_success": True,
        "screenshot_on_step_failure": True
        }
    )

    def navigate_to_home():
        browser.implicitly_wait(TIMEOUT)
        browser.get(url)
    
    url_list = [
        "https://d2h3ljlsmzojxz.cloudfront.net/",
        "https://youtube.com"
    ]

    for index, url in enumerate(url_list):
        await webdriver.execute_step("URL check {}".format(index), navigate_to_home)

    logger.info("---------Finished the execution---------")

    
async def handler(event, context):
    return await main()

标签: pythonamazon-web-servicesselenium-webdriveramazon-cloudwatch

解决方案


您当前的代码正在尝试使用返回的值navigate_to_home(url)作为回调函数。这里的值是None

而是尝试以下方法,以确保传入正确的回调函数:

await webdriver.execute_step("Navigate to home", lambda: navigate_to_home(url))


推荐阅读