Selenium中的driver.quit()和driver.close()出现错误。

huangapple go评论131阅读模式
英文:

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(&quot;chromedriver.exe&quot;)

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: &lt;urllib3.connection.HTTPConnection object at 0x000002083D14DC60&gt;: 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=&#39;localhost&#39;, port=61012): Max retries exceeded with url: /session/17e2df6fcf1ef1c5281fe3b84195b8dd/url (Caused by NewConnectionError(&#39;&lt;urllib3.connection.HTTPConnection object at 0x000002083D14DC60&gt;: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it&#39;))

答案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:

  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.

  2. 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(&quot;chromedriver.exe&quot;)
    driver.get(...)

while True:
    input()
    login()
    driver.quit()

huangapple
  • 本文由 发表于 2023年3月7日 03:13:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75654918.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定