selenium ConnectionRefusedError:python 3中的[WinError 10061]

时间:2018-04-15 21:17:46

标签: python selenium

我有一个python程序,它使用selenium转到某个站点,在输入框中输入一个值,然后获取返回值。现在,我有一个字符串列表,我将值添加到url的末尾,程序在for循环中处理它。问题是,在第一次运行循环后,它会出错:

  

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

我在一篇类似的帖子中读到了#34;浏览器在交互式shell&#34;中启动后不能关闭,但当我删除行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

这是我的代码:

import time
import random as r
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

browser = webdriver.Chrome()

things = [list]


cNow = 'Angstroms'
for item in things:
    newurl = url
    print(newurl)
    browser.get(newurl)

    cfrom = cNow
    ufrom = item

    box = browser.find_element_by_id('right')

    elem = browser.find_element_by_name('textfieldName')  # Find the search box
    elem.send_keys('1' + Keys.RETURN)

    ow = box.get_attribute('value')
    print(ow)
    browser.quit()

任何想法都将不胜感激。感谢。

1 个答案:

答案 0 :(得分:1)

好的,我发现了问题。问题是,browser.quit()行是在循环中,当它不应该是。对不起,对不起!