self.driver_.get("http://www.stockbroking.com.my") 时出现 Selenium WebDriverException

Selenium WebDriverException when self.driver_.get("http://www.stockbroking.com.my")

我有一个名为“Kenbot”的 Ken**** 股票购买机器人(Ken**** 是一家股票经纪公司)已经 运行 一年了,我已经花了大部分时间尝试消除错误的时间。 直到最近,我遇到了一个新的追溯问题。它只在某些日子偶尔发生,而在其他日子则不会。

Kenbot 通过我的 Synology NAS 在 docker 图像(selenium/standalone-chrome:最新)上运行,每天早上在市场开盘前它会下订单购买少量股票。它通过 Selenium 工作。

现在除了使用 @retry 装饰器,如果遇到 WebDriverException 将重新尝试下股票订单,我没有其他解决方案。

我该如何解决这个问题?

Traceback (most recent call last):
  File "/volume1/homes/admin/Drive/stock/order/downloadContractNote.py", line 23, in main
    with docker_selenium() as _, Kenbot(headless=True) as bot:
  File "/volume1/homes/admin/Drive/stock/logs/logger.py", line 58, in wrapper
    return func(*args, **kwargs)
  File "/volume1/homes/admin/Drive/stock/order/kenbot.py", line 100, in __init__
    self.bot.login()
  File "/volume1/homes/admin/Drive/stock/order/kenbot.py", line 70, in f_retry
    return f(*args, **kwargs)
  File "/volume1/homes/admin/Drive/stock/order/kenbot.py", line 176, in login
    self.driver_.get("http://www.stockbroking.com.my")
  File "/volume1/homes/admin/venv/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 333, in get
    self.execute(Command.GET, {'url': url})
  File "/volume1/homes/admin/venv/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/volume1/homes/admin/venv/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: net::ERR_NAME_NOT_RESOLVED
  (Session info: headless chrome=94.0.4606.81)

假设你使用的是docker-compose,你可以添加www.stockbroking.com.my的IP:

...
services:
  <app>:    
    extra_hosts:
    - "www.stockbroking.com.my:<add IP address here>"
...

或者,docker run --add-host www.stockbroking.com.my:<add IP address here> ...

或使用 Python docker SDK,如评论中所示:

container = client.containers.run('selenium/standalone-chrome', user = 'root', detach=True, auto_remove=True, environment = ["TZ=Asia/Kuala_Lumpur"], ports={'4444/tcp':('127.0.0.1',4444)}, extra_hosts={'www.stockbroking.com.my': '999.99.99.999'}, privileged = True)