selenium ConnectionRefusedError:Python 3 中的 [WinError 10061]

问题描述 投票:0回答:2

我有一个 python 程序,它使用 selenium 访问站点并获取元素的值。现在,我有一个字典列表,每个字典都有一个

url
参数。我遍历列表并转到每个字典的 url。问题是,在第一次运行循环后,它给了我一个错误:

ConnectionRefusedError: [WinError 10061] 由于目标机器主动拒绝而无法建立连接

Traceback (most recent call last):
  File "C:\Users\Me\Desktop\mfile\getfuncs.py", line 15, in <module>
    browser.get(newurl)
  File "C:\Users\Me\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 324, in get
    self.execute(Command.GET, {'url': url})
  File "C:\Users\Me\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 310, in execute
    response = self.command_executor.execute(driver_command, params)
  File "C:\Users\Me\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 466, in execute
    return self._request(command_info[0], url, body=data)
  File "C:\Users\Me\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 489, in _request
    self._conn.request(method, parsed_url.path, body, headers)
  File "C:\Users\Me\AppData\Local\Programs\Python\Python36-32\lib\http\client.py", line 1239, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "C:\Users\Me\AppData\Local\Programs\Python\Python36-32\lib\http\client.py", line 1285, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "C:\Users\Me\AppData\Local\Programs\Python\Python36-32\lib\http\client.py", line 1234, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "C:\Users\Me\AppData\Local\Programs\Python\Python36-32\lib\http\client.py", line 1026, in _send_output
    self.send(msg)
  File "C:\Users\Me\AppData\Local\Programs\Python\Python36-32\lib\http\client.py", line 964, in send
    self.connect()
  File "C:\Users\Me\AppData\Local\Programs\Python\Python36-32\lib\http\client.py", line 936, in connect
    (self.host,self.port), self.timeout, self.source_address)
  File "C:\Users\Me\AppData\Local\Programs\Python\Python36-32\lib\socket.py", line 724, in create_connection
    raise err
  File "C:\Users\Me\AppData\Local\Programs\Python\Python36-32\lib\socket.py", line 713, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it

我在类似的文章中读到“在交互式外壳中启动后不得关闭浏览器”,但是当我删除该行

browser.quit()
时,它给出了另一个错误:

ConnectionAbortedError:[WinError 10053] 已建立的连接被主机中的软件中止

Traceback (most recent call last):
  File "C:\Users\Me\Desktop\mfile\getfuncs.py", line 15, in <module>
    browser.get(newurl)
  File "C:\Users\Me\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 324, in get
    self.execute(Command.GET, {'url': url})
  File "C:\Users\Me\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 310, in execute
    response = self.command_executor.execute(driver_command, params)
  File "C:\Users\Me\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 466, in execute
    return self._request(command_info[0], url, body=data)
  File "C:\Users\Me\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 489, in _request
    self._conn.request(method, parsed_url.path, body, headers)
  File "C:\Users\Me\AppData\Local\Programs\Python\Python36-32\lib\http\client.py", line 1239, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "C:\Users\Me\AppData\Local\Programs\Python\Python36-32\lib\http\client.py", line 1285, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "C:\Users\Me\AppData\Local\Programs\Python\Python36-32\lib\http\client.py", line 1234, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "C:\Users\Me\AppData\Local\Programs\Python\Python36-32\lib\http\client.py", line 1065, in _send_output
    self.send(chunk)
  File "C:\Users\Me\AppData\Local\Programs\Python\Python36-32\lib\http\client.py", line 986, in send
    self.sock.sendall(data)
ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine

我还检查了我的防火墙,它没有阻止硒。

这是我的代码:

from selenium import webdriver

browser = webdriver.Chrome()

things = [list of dicts]
results = []
    
for item in things:
    browser.get(item['url'])

    box = browser.find_element_by_id('some element')
    content = box.get_attribute('value')
    
    results.append(content)
    browser.quit()

是什么导致了这个问题?

python selenium
2个回答
12
投票

我发现问题了;

browser.quit()
在不应该的情况下处于循环中。


0
投票

谢谢!疯狂的是,我来到这里寻找问题,而且我也在循环中使用了 driver.quit() 。巧合

© www.soinside.com 2019 - 2024. All rights reserved.