簡體   English   中英

selenium ConnectionRefusedError:python 3 中的 [WinError 10061]

[英]selenium ConnectionRefusedError: [WinError 10061] in python 3

我有一個 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

我在一篇類似的文章中讀到“在交互式 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()

是什么導致了這個問題?

我發現了問題; browser.quit()在不應該出現的情況下處於循環中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM