【问题标题】:AWS EC2 times out when trying to authenticate using a selenium headless chrome尝试使用 selenium 无头 chrome 进行身份验证时,AWS EC2 超时
【发布时间】:2021-05-24 12:57:01
【问题描述】:

我正在尝试使用 AWS 来自动化一些程序,并且需要使用我正在使用的 API 对自己进行身份验证。问题是,它的速度太慢了。我正在使用 selenium 无头 chrome 浏览器连接到页面,填写我需要的信息并提交表单(根据其他来源,我发现 TD Ameritrade API 只接受浏览器路由,所以没有办法做到这一点有请求,如果你知道,那也很好)。在页面上查找元素大约需要 20 秒,发送密钥可能需要一整分钟,然后尝试提交表单,它会在 5 分钟后超时。这是我的 selenium 实现的问题,还是与 AWS 或我连接的 API 有关?我的代码如下图,报错:

        oauth = OAuth2Session(client_code, redirect_uri=redirect)
        authorization_url, state = oauth.authorization_url('https://auth.tdameritrade.com/auth')
        
        chrome_options = Options()
        chrome_options.add_argument('--headless')
        chrome_options.add_argument('--no-sandbox')
        #chrome_options.add_argument('--disable-gpu')
        chrome_options.add_argument('--window-size=1280x1696')
        chrome_options.add_argument('--user-data-dir=tmp/user-data')
        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('--data-path=tmp/data-path')
        chrome_options.add_argument('--ignore-certificate-errors')
        chrome_options.add_argument('--homedir=tmp')
        chrome_options.add_argument('--disk-cache-dir=tmp/cache-dir')
        chrome_options.add_argument('--no-proxy-server')
        chrome_options.add_argument("--proxy-server='direct://'");
        chrome_options.add_argument("--proxy-bypass-list=*");
        chrome_options.add_argument('user-agent=Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36')
        chrome_options.binary_location = "/usr/bin/chromium-browser"
        
        with webdriver.Chrome(options=chrome_options) as driver:
            driver.get(authorization_url)
            
            usernamebox = driver.find_element_by_id("username0")
            print("found")
            usernamebox.send_keys(username)
            print("sent")
            passbox = driver.find_element_by_id("password")
            print("found2")
            passbox.send_keys(password)
            print("sent2")
            passbox.submit()
            print("submitted")

错误:

Traceback (most recent call last):
  File "/home/ubuntu/environment/td_ameritrade_test.py", line 229, in <module>
    td = TDClient()
  File "/home/ubuntu/environment/td_ameritrade_test.py", line 172, in __init__
    self.oauth_client = self.authenticate_user()
  File "/home/ubuntu/environment/td_ameritrade_test.py", line 90, in authenticate_user
    passbox.submit()
  File "/home/linuxbrew/.linuxbrew/Cellar/python@3.8/3.8.6/lib/python3.8/site-packages/selenium/webdriver/remote/webelement.py", line 86, in submit
    self._parent.execute_script(
  File "/home/linuxbrew/.linuxbrew/Cellar/python@3.8/3.8.6/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 634, in execute_script
    return self.execute(command, {
  File "/home/linuxbrew/.linuxbrew/Cellar/python@3.8/3.8.6/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/home/linuxbrew/.linuxbrew/Cellar/python@3.8/3.8.6/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: timeout: Timed out receiving message from renderer: 300.000
  (Session info: headless chrome=86.0.4240.75)

任何帮助将不胜感激!

【问题讨论】:

  • 你真的需要那么多add_argument吗?
  • 这些是我在 AWS 中使用 selenium 的唯一指南中找到的选项,尽管这些指南都描述了网络爬虫,但您的权利其中一些论点可能毫无用处。您认为这会减慢速度吗?
  • 粗略设置太多参数会对测试执行产生负面影响。根据 Test Specs 使用参数。
  • 好吧,我不知道 Stack Overflow 是如何工作的,但是这很神奇,我只使用了 headless 参数,而且效果很好。非常感谢。

标签: python selenium google-chrome amazon-ec2 selenium-chromedriver


【解决方案1】:

有关您的用例的更多详细信息将有助于我们分析程序执行速度过慢的原因。以下是一些注意事项:

  • Chrome 在启动期间崩溃的一个常见原因是在 Linux 上以 root 用户 (administrator) 的身份运行 Chrome。虽然可以通过在创建 WebDriver 会话时传递 --no-sandbox 标志来解决此问题,但不支持并且强烈建议不要使用此类配置。您需要将环境配置为以普通用户身份运行 Chrome。因此,您可能需要删除 --no-sandbox 选项。

这是Sandbox故事的链接。

  • 使用--headless 选项时,由于某些限制,您将无法使用--window-size=1280x1696

您可以在以下位置找到一些相关的详细讨论:

您可以在ERROR:gpu_process_transport_factory.cc(1007)-Lost UI shared context : while initializing Chrome browser through ChromeDriver in Headless mode找到相关的详细讨论

  • 另外你还没有提到使用--hide-scrollbars--enable-logging--log-level=0--v=99--single-process--single-process--data-path=tmp/data-path--homedir=tmp--disk-cache-dir=tmp/cache-dir、@9876543的任何具体要求--proxy-server='direct://'--proxy-bypass-list=* 参数,您选择暂时放弃并根据您的测试规范重新添加它们。

参考

您可以在以下位置找到一些相关的详细讨论:

【讨论】:

    猜你喜欢
    • 2019-12-21
    • 2015-12-25
    • 2022-06-20
    • 2020-12-08
    • 2018-06-05
    • 2022-06-10
    • 2018-11-16
    • 2019-09-28
    • 2021-08-16
    相关资源
    最近更新 更多