问题
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