【问题标题】:selenium ConnectionRefusedError: [WinError 10061] in python 3selenium ConnectionRefusedError:python 3 中的 [WinError 10061]
【发布时间】:2018-09-25 13:51:51
【问题描述】:

我有一个使用 selenium 访问站点并获取元素值的 python 程序。现在,我有一个字典列表,每个字典都有一个 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

我在类似的帖子中读到“在交互式 shell 中启动后不得关闭浏览器”,但是当我删除 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

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

这是我的代码:

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()

是什么导致了这个问题?

【问题讨论】:

  • urlherenewurlthings = [list_here]更新问题以供进一步分析

标签: python selenium


【解决方案1】:

我发现了问题; browser.quit() 不应该在循环中。

【讨论】:

  • 我遇到了同样的问题,但在我的情况下,它是 browser.close()。这对我造成了同样的错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-06-07
  • 2018-10-01
  • 2022-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多