首页 > 解决方案 > Chromedriver:Chrome 连接因 Docker 容器中的硒而失败

问题描述

我想运行下面的代码并使用 selenium 连接到 chrome。我在 PATH 中安装并chromium-browser安装了 chromedriver 91.0.4472.101。我错过了要安装的东西吗?是访问 id/pw 错误吗?任何提示将不胜感激。

编码:

   @retry(tries=4, delay=2, backoff=2,exceptions=(RetryException))
    def oneTry(self,fromd,tod,exit=0):
        try :
            self.openSearchPage()
            self.prep(fromd.strftime("%d/%m/%Y"),tod.strftime("%d/%m/%Y"),exit)
            self.searchClick()
            self.checkTableHTML()
            fromdstr = fromd.strftime("%Y/%m/%d")
            todstr = tod.strftime("%Y/%m/%d")
            print("[ from: {} - to: {} ]".format(fromdstr,todstr), end=" => ")
            self.waitLoading()
            self.buttonClick()
            
            # time.sleep(random.uniform(20,120))
            time.sleep(2)
            filedf = pd.DataFrame([[x.name,datetime.fromtimestamp(x.stat().st_ctime)] for x in self.path.glob("*.xls")],columns=["name","timestamp"])
            filedf = filedf.sort_values(["timestamp"],ascending=False).reset_index(drop=True)
            tmppath = Path(filedf.name[0])
            tmppath.rename("DOWN_{}_{}_{}.xls".format(datetime.now().strftime("%Y%m%d_%H%M%S"),fromdstr.replace("/","-"),todstr.replace("/","-")))
        except Exception as e :
            print(e.args)
            print(".... FAIL ... |")
            raise RetryException

错误:

ConnectionRefusedError                    Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/urllib3/connection.py in _new_conn(self)
    169             conn = connection.create_connection(
--> 170                 (self._dns_host, self.port), self.timeout, **extra_kw

/usr/local/lib/python3.6/dist-packages/urllib3/util/connection.py in create_connection(address, timeout, source_address, socket_options)
     85                 sock.bind(source_address)
---> 86             sock.connect(sa)
     87             return sock

ConnectionRefusedError: [Errno 111] Connection refused
 ...
/usr/local/lib/python3.6/dist-packages/urllib3/connectionpool.py in _make_request(self, conn, method, url, timeout, chunked, **httplib_request_kw)
    393             else:
--> 394                 conn.request(method, url, **httplib_request_kw)

/usr/local/lib/python3.6/dist-packages/urllib3/connection.py in request(self, method, url, body, headers)
    233             headers["User-Agent"] = _get_default_user_agent()
--> 234         super(HTTPConnection, self).request(method, url, body=body, headers=headers)

/usr/lib/python3.6/http/client.py in request(self, method, url, body, headers, encode_chunked)
   1280         """Send a complete request to the server."""
-> 1281         self._send_request(method, url, body, headers, encode_chunked)

/usr/lib/python3.6/http/client.py in _send_request(self, method, url, body, headers, encode_chunked)
   1326             body = _encode(body, 'body')
-> 1327         self.endheaders(body, encode_chunked=encode_chunked)

/usr/local/lib/python3.6/dist-packages/urllib3/connection.py in connect(self)
    199     def connect(self):
--> 200         conn = self._new_conn()
    201         self._prepare_conn(conn)
/usr/local/lib/python3.6/dist-packages/urllib3/connection.py in _new_conn(self)
    181             raise NewConnectionError(
--> 182                 self, "Failed to establish a new connection: %s" % e
NewConnectionError: <urllib3.connection.HTTPConnection object at 0x7f76487e2b38>: Failed to establish a new connection: [Errno 111] Connection refused

标签: dockerselenium-chromedriver

解决方案


推荐阅读