Python AWS Canary Selenium用户代理字符串

Python AWS Canary Selenium用户代理字符串,python,amazon-web-services,selenium,amazon-cloudwatch,Python,Amazon Web Services,Selenium,Amazon Cloudwatch,我希望在AWS Selenium Canary中设置一个自定义用户代理,但不知何故,我试图做的事情不起作用 根据此文档,我应该能够更改/附加一个字符串到浏览器的用户代理头 add_user_agent(user_agent_str) Appends the value of user_agent_str to the browser's user agent header. You must assign user_agent_str before creating the browser in

我希望在AWS Selenium Canary中设置一个自定义用户代理,但不知何故,我试图做的事情不起作用

根据此文档,我应该能够更改/附加一个字符串到浏览器的用户代理头

add_user_agent(user_agent_str)
Appends the value of user_agent_str to the browser's user agent header. You must assign user_agent_str before creating the browser instance.

Example:

synthetics_webdriver.add_user_agent('MyApp-1.0')

这是我的示例代码

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


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

    synthetics_webdriver.add_user_agent('My User Agent String')
    driver = synthetics_webdriver.Chrome()
        
    driver.get('myurl.com')
    user_agent = driver.execute_script('return navigator.userAgent')
    # should be/contain 'My User Agent String' but instead looks like 'CloudWatchSynthetics-arn'
    logger.info('This is the user agent string: {}'.format(user_agent))
    
    # selenium python tests

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

我已经做了很多调试,确保该方法存在,将该方法移动到文件的顶部,但我没有任何运气让它工作


非常感谢您提供的任何帮助。

据我所知,您需要先创建一个对象,以执行
添加用户代理
方法并保存该状态

我没有环境,所以请尝试一下,并在评论中告诉我结果

syn_wdriver=synthetics_webdriver
#syn_wdriver=synthetics_webdriver()#如果上述方法无效,请尝试此方法,我不确定syn_webdriver对象
syn_wdriver.add_user_agent(“我的用户代理字符串”)
driver=syn_wdriver.Chrome()
或:

driver=synthetics\u webdriver.add\u user\u agent(“我的用户代理字符串”).Chrome()

我又一次沉迷于自己的代码,意识到自己犯了一个简单的错误

这就是我的结构

def myfunction():
    # my selenium test

myfunction()
但是,当我执行此操作时,add_user_agent函数出于某种原因不会向浏览器用户代理头中注入字符串

当我把代码改成这个的时候

def myfunction():
    # selenium tests
webdriver.execute_step('StepName', myfunction)
它工作得很好