英文:
Selenium error with driver.quit() and driver.close()
问题
我有以下的代码:
from selenium import webdriver
driver = webdriver.Chrome("chromedriver.exe")
def login():
driver.get(...)
当我输入一些内容时,浏览器会打开,执行login()函数,然后关闭浏览器。但当我第二次输入内容时,我会得到错误:
ConnectionRefusedError: [WinError 10061] 由于目标计算机积极拒绝,无法建立连接
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x000002083D14DC60>: 无法建立新连接:[WinError 10061] 由于目标计算机积极拒绝,无法建立连接
在处理上述异常时,另一个异常发生了:
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='localhost', port=61012): 最大重试次数已超过,URL为: /session/17e2df6fcf1ef1c5281fe3b84195b8dd/url (由于NewConnectionError('<urllib3.connection.HTTPConnection object at 0x000002083D14DC60>: 无法建立新连接:[WinError 10061] 由于目标计算机积极拒绝,无法建立连接')引起)
英文:
I have the following code:
from selenium import webdriver
driver = webdriver.Chrome("chromedriver.exe")
def login():
driver.get(...)
while True:
input()
login()
driver.quit()
driver.close()
Let me explain:
I want when I type something, the browser opens, login() executes, and the browser closes. But when I enter something a second time, I get errors:
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x000002083D14DC60>: 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:
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='localhost', port=61012): Max retries exceeded with url: /session/17e2df6fcf1ef1c5281fe3b84195b8dd/url (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x000002083D14DC60>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it'))
答案1
得分: 0
以下是要翻译的代码部分:
There are a couple issues:
1. You are trying to close the browser after quitting the driver instance. `.close()` closes the browser tab, `.quit()` completely quits the browser instance. You don't need both, `.quit()` will take care of both.
1. You are quitting the driver instance INSIDE your loop but never creating a new browser instance. You either need to pull the `.quit()` outside the loop or create a new browser instance inside the loop. It sounds like you want to do the latter.
The updated code would look like
from selenium import webdriver
driver = None
def login():
driver = webdriver.Chrome("chromedriver.exe")
driver.get(...)
while True:
input()
login()
driver.quit()
英文:
There are a couple issues:
-
You are trying to close the browser after quitting the driver instance.
.close()closes the browser tab,.quit()completely quits the browser instance. You don't need both,.quit()will take care of both. -
You are quitting the driver instance INSIDE your loop but never creating a new browser instance. You either need to pull the
.quit()outside the loop or create a new browser instance inside the loop. It sounds like you want to do the latter.
The updated code would look like
from selenium import webdriver
driver = None
def login():
driver = webdriver.Chrome("chromedriver.exe")
driver.get(...)
while True:
input()
login()
driver.quit()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论