Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

selenium ConnectionRefusedError: [WinError 10061] in python 3

I have a python program that uses selenium to go to a site and get the value of an element. Now, I have a list of dicts, each one having a url parameter. I iterate through the list and go to the url from each dict. The problem is that after the first run of the loop it gives me an error:

ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it

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

I read in a similar post that "The browser must not be closed after being launched in the interactive shell", but when I remove the line browser.quit(), it gives another error:

ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine

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

I've also checked my firewall, and it's not blocking selenium.

Here is my code:

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

What's causing this problem?

like image 464
marsnebulasoup Avatar asked Sep 05 '26 06:09

marsnebulasoup


1 Answers

I've found the problem; the browser.quit() was in the loop when it was not supposed to be.

like image 111
marsnebulasoup Avatar answered Sep 06 '26 20:09

marsnebulasoup



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!