我正在使用以下selenium代码:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
options=Options()
driver=webdriver.Chrome(options=options)
i = 0
while True:
driver.get("https://www.google.com/")
time.sleep(2)
driver.quit()
i = i + 1
这个想法是,chrome浏览器打开和关闭在一个无限循环。当我运行它时,我得到一个"最大重试错误"。如何解决这个问题?
第一次执行driver.quit()
时,它将终止浏览器实例。对于第二次迭代,必须执行的第一行将是driver.get("https://www.google.com/")
,现在,由于驱动程序对象在第一次迭代中被杀死,所以驱动程序在第二次迭代中为空。导致
sock.connect(sa)
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
During handling of the above exception, another exception occurred:
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x000001D12A2671C0>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it
During handling of the above exception, another exception occurred:
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='127.0.0.1', port=57911): Max retries exceeded with url: /session/eedcbdf06a0db68d2b4b6e9bc76b5167/url (Caused by NewConne
要解决这个问题,
解决方案:您应该为每个迭代创建一个驱动程序实例。
i = 0
while True:
driver = webdriver.Chrome(options=options)
driver.get("https://www.google.com/")
time.sleep(2)
driver.quit()
i = i + 1