urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection …>: Failed to establish a new connection: [Errno 111] Connection refused

风流意气都作罢 提交于 2020-12-07 04:55:57

问题


Why does one see this error in Selenium;

urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0xsome_hex_address>: Failed to establish a new connection: [Errno 111] Connection refused

When using various functions?


回答1:


Please ensure you have not somewhere terminated your session. For example (in Python);

import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By

driver = webdriver.Chrome('/usr/bin/chromedriver',options=chrome_options)
driver.get('https://www.somewebsite.com')
try:
  WebDriverWait(driver,20).until(EC.presence_of_element_located((By.XPATH,"//input[@type='file']")))
finally:
  driver.quit()
WebDriverWait(driver,20).until(EC.presence_of_element_located((By.CLASS,"someclass")))

The second WebDriverWait will fail with the said error message because the "finally" clause of the "try" is always executed, and thus the driver quit/was closed. Hence, the subsequent WebDriverWait failed. Easy to miss.

By the way, in the above example you could change the "finally:" to "except:" to 'only' do a driver.quit() if the try fails.



来源:https://stackoverflow.com/questions/56424704/urllib3-exceptions-newconnectionerror-urllib3-connection-httpconnection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!