如何解决使用 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_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()
解决方法
您当前的代码正在尝试使用 navigate_to_home(url)
返回的值作为回调函数。此处该值为 None
。
请尝试以下操作以确保传入正确的回调函数:
await webdriver.execute_step("Navigate to home",lambda: navigate_to_home(url))
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。