【问题标题】:Unable to consecutively launch a new chrome session [duplicate]无法连续启动新的 chrome 会话 [重复]
【发布时间】:2020-08-27 11:53:07
【问题描述】:

我正在尝试打开一个网站,关闭浏览器会话,然后在新窗口中打开该网站。但是在第一个浏览器会话关闭后出现错误。

浏览器:Chrome

浏览器版本:81.0.4044.138

Chrome 驱动程序版本:81.0.4044.138

这是我的代码

from selenium import webdriver
import time

driver = webdriver.Chrome('/Users/n/Downloads/chromedriver')

driver.get('https://stackoverflow.com/')
driver.maximize_window()

driver.find_element_by_xpath('/html/body/header/div/ol[2]/li[2]/a[1]').click()
time.sleep(5)

driver.quit()


driver.get('https://stackoverflow.com/')
driver.maximize_window()

这是我得到的错误

Traceback (most recent call last):
  File "/Users/n/PycharmProjects/Selenium/venv/lib/python3.7/site-packages/urllib3/connection.py", line 160, in _new_conn
    (self._dns_host, self.port), self.timeout, **extra_kw
  File "/Users/n/PycharmProjects/Selenium/venv/lib/python3.7/site-packages/urllib3/util/connection.py", line 84, in create_connection
    raise err
  File "/Users/n/PycharmProjects/Selenium/venv/lib/python3.7/site-packages/urllib3/util/connection.py", line 74, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [Errno 61] Connection refused

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/n/PycharmProjects/Selenium/venv/lib/python3.7/site-packages/urllib3/connectionpool.py", line 677, in urlopen
    chunked=chunked,
  File "/Users/n/PycharmProjects/Selenium/venv/lib/python3.7/site-packages/urllib3/connectionpool.py", line 392, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 1244, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 1290, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 1239, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 1026, in _send_output
    self.send(msg)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 966, in send
    self.connect()
  File "/Users/n/PycharmProjects/Selenium/venv/lib/python3.7/site-packages/urllib3/connection.py", line 187, in connect
    conn = self._new_conn()
  File "/Users/n/PycharmProjects/Selenium/venv/lib/python3.7/site-packages/urllib3/connection.py", line 172, in _new_conn
    self, "Failed to establish a new connection: %s" % e
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x1079f9b10>: Failed to establish a new connection: [Errno 61] Connection refused

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/n/PycharmProjects/Selenium/venv/Automation/Testing/src_phase4/stack_test.py", line 15, in <module>
    driver.get('https://stackoverflow.com/')
  File "/Users/n/PycharmProjects/Selenium/venv/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 333, in get
    self.execute(Command.GET, {'url': url})
  File "/Users/n/PycharmProjects/Selenium/venv/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 319, in execute
    response = self.command_executor.execute(driver_command, params)
  File "/Users/n/PycharmProjects/Selenium/venv/lib/python3.7/site-packages/selenium/webdriver/remote/remote_connection.py", line 374, in execute
    return self._request(command_info[0], url, body=data)
  File "/Users/n/PycharmProjects/Selenium/venv/lib/python3.7/site-packages/selenium/webdriver/remote/remote_connection.py", line 397, in _request
    resp = self._conn.request(method, url, body=body, headers=headers)
  File "/Users/n/PycharmProjects/Selenium/venv/lib/python3.7/site-packages/urllib3/request.py", line 80, in request
    method, url, fields=fields, headers=headers, **urlopen_kw
  File "/Users/n/PycharmProjects/Selenium/venv/lib/python3.7/site-packages/urllib3/request.py", line 171, in request_encode_body
    return self.urlopen(method, url, **extra_kw)
  File "/Users/n/PycharmProjects/Selenium/venv/lib/python3.7/site-packages/urllib3/poolmanager.py", line 336, in urlopen
    response = conn.urlopen(method, u.request_uri, **kw)
  File "/Users/n/PycharmProjects/Selenium/venv/lib/python3.7/site-packages/urllib3/connectionpool.py", line 765, in urlopen
    **response_kw
  File "/Users/n/PycharmProjects/Selenium/venv/lib/python3.7/site-packages/urllib3/connectionpool.py", line 765, in urlopen
    **response_kw
  File "/Users/n/PycharmProjects/Selenium/venv/lib/python3.7/site-packages/urllib3/connectionpool.py", line 765, in urlopen
    **response_kw
  File "/Users/n/PycharmProjects/Selenium/venv/lib/python3.7/site-packages/urllib3/connectionpool.py", line 725, in urlopen
    method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]
  File "/Users/n/PycharmProjects/Selenium/venv/lib/python3.7/site-packages/urllib3/util/retry.py", line 439, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='127.0.0.1', port=57825): Max retries exceeded with url: /session/2808efbb67f09baa943cc4acda452537/url (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x1079f9b10>: Failed to establish a new connection: [Errno 61] Connection refused'))

Process finished with exit code 1

当我初始化一个新的驱动程序变量并使用它打开一个新的 chrome 窗口时,问题就消失了。

driver2 = webdriver.Chrome('/Users/n/Downloads/chromedriver')

driver2.get('https://stackoverflow.com/')

driver2.maximize_window()

【问题讨论】:

    标签: python python-3.x selenium selenium-webdriver


    【解决方案1】:

    您可以尝试在 quit() 之后使用其 init 方法重新初始化 webdriver 的实例。像这样:

    from selenium import webdriver
    import time
    
    driver = webdriver.Chrome('/Users/n/Downloads/chromedriver')
    
    driver.get('https://stackoverflow.com/')
    driver.maximize_window()
    
    driver.find_element_by_xpath('/html/body/header/div/ol[2]/li[2]/a[1]').click()
    time.sleep(5)
    
    driver.quit()
    
    driver.__init__()
    
    
    driver.get('https://stackoverflow.com/')
    driver.maximize_window()
    

    【讨论】:

    • 非常感谢您。你知道为什么会发生这种情况吗?
    猜你喜欢
    • 2022-08-06
    • 1970-01-01
    • 1970-01-01
    • 2018-06-27
    • 1970-01-01
    • 1970-01-01
    • 2021-10-10
    • 1970-01-01
    • 2023-03-10
    相关资源
    最近更新 更多