Search code examples
pythonazureselenium-webdriverselenium-chromedriverazure-databricks

How to solve the following MaxRetryError: "TTPConnectionPool(host='localhost', port=48605): Max retries exceeded with url:"


I'm using the selenium library with python and I'm working on Databricks (Azure). I'm trying to connect to a web page to take values and I can do it the first time. From the second time, the following error is shown:

HTTPConnectionPool(host='localhost', port=48605): Max retries exceeded with url: /session/c92f4b5467e1d376d6c98b21d3b0da19/element (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f72148c5e40>: Failed to establish a new connection :[ Errno 111] Connection refused'))

Therefore the first time works well, but the second time, the error is shown and I have to run it all again and It will work.

The google Chrome version is 119.0.6045.160 (64 bits)

CODE:

s = Service('/tmp/chrome/latest/chromedriver_linux64/chromedriver', port = 48605)
options = webdriver.ChromeOptions()
options.binary_location = "/tmp/chrome/latest/chrome-linux/chrome"
options.add_argument('headless')
options.add_argument('--disable-infobars')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--no-sandbox')
options.add_argument('--remote-debugging-port=49035')
options.add_argument('--homedir=/tmp/chrome/chrome-user-data-dir')
options.add_argument('--user-data-dir=/tmp/chrome/chrome-user-data-dir')
prefs = {"download.default_directory":"/tmp/chrome/chrome-user-data-di",
         "download.prompt_for_download":False
}
options.add_experimental_option("prefs",prefs)
driver = webdriver.Chrome(service=s, options=options)
driver.maximize_window() # For maximizing window
driver.implicitly_wait(20)

driver.get('https://muisca.dian.gov.co/WebRutMuisca/DefConsultaEstadoRUT.faces')

text_Box = driver.find_element(By.XPATH, '//*[@id="vistaConsultaEstadoRUT:formConsultaEstadoRUT:numNit"]')
text_Box.click()
time.sleep(4)
NIT = driver.find_element(By.NAME, "vistaConsultaEstadoRUT:formConsultaEstadoRUT:numNit")
NIT.send_keys('123456789')
time.sleep(4)
btn_buscar = driver.find_element(By.XPATH, '//*[@id="vistaConsultaEstadoRUT:formConsultaEstadoRUT:btnBuscar"]')
btn_buscar.click()
time.sleep(4)
primerApellido = driver.find_element(By.XPATH, '//*[@id="vistaConsultaEstadoRUT:formConsultaEstadoRUT:primerApellido"]')
time.sleep(4)
segundoApellido = driver.find_element(By.XPATH, '//*[@id="vistaConsultaEstadoRUT:formConsultaEstadoRUT:segundoApellido"]')
time.sleep(4)
primerNombre = driver.find_element(By.XPATH, '//*[@id="vistaConsultaEstadoRUT:formConsultaEstadoRUT:primerNombre"]')
time.sleep(4)
otrosNombres = driver.find_element(By.XPATH, '//*[@id="vistaConsultaEstadoRUT:formConsultaEstadoRUT:otrosNombres"]')
time.sleep(4)
print(str(primerApellido.text) + " " + str(segundoApellido.text) + " " + str(primerNombre.text) + str(otrosNombres.text))

driver.quit()

I want to access n times to the web pages and take their values without the error is showed


Solution

  • That is because you are ending the connection at the last line:

    driver.quit()

    enter image description here

    So, this is the reason you need to run it all again to reestablish the connection.

    To avoid this error, close the connection after performing all your actions.

    Additionally, you also encounter this error if the process is terminated or killed.

    s.process.kill() or s.process.terminate().

    Ensure that you have not executed the above statements before running your action statements.