How to handle colon (:) in the name when using Selenium in Python How to handle colon (:) in the name when using Selenium in Python selenium selenium

How to handle colon (:) in the name when using Selenium in Python


The name attribute of the <input> element contains the : character as in:

vistaConsultaEstadoRUT:formConsultaEstadoRUT:numNit

And : bears a special effect when used within a . Hence your program fails to find the desired element and raises NoSuchElementException


Solution

To find the element you can use either of the following Locator Strategies:

  • Using css_selector:

    driver.find_element_by_css_selector("input[name='vistaConsultaEstadoRUT:formConsultaEstadoRUT:numNit']")
  • Using xpath:

    driver.find_element_by_xpath("//input[@name='vistaConsultaEstadoRUT:formConsultaEstadoRUT:numNit']")

Best practices

As you are invoking send_keys() ideally you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='vistaConsultaEstadoRUT:formConsultaEstadoRUT:numNit']"))).send_keys("860003020")
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@name='vistaConsultaEstadoRUT:formConsultaEstadoRUT:numNit']"))).send_keys("860003020")
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support import expected_conditions as EC
  • Browser Snapshot:

muisca


Reference

You can find a couple of relevant discussions in: