Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am trying to search a number in a web page (https://muisca.dian.gov.co/WebRutMuisca/DefConsultaEstadoRUT.faces).

I know the name of the input element is: "vistaConsultaEstadoRUT:formConsultaEstadoRUT:numNit", however when I try to find the element I got this error:

"NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"[name="vistaConsultaEstadoRUT:formConsultaEstadoRUT:numNit"]"}
  (Session info: chrome=81.0.4044.138)"

This is what I have tried:

from selenium import webdriver
driver = webdriver.Chrome("C:\\Users\\jcherrerab\\Anaconda3\\Lib\\site-packages\\selenium\\webdriver\\chrome\\chromedriver.exe")
driver.get("https://muisca.dian.gov.co/WebRutMuisca/DefConsultaEstadoRUT.faces")
driver.find_element_by_name("vistaConsultaEstadoRUT:formConsultaEstadoRUT:numNit").send_keys("860003020")

Can you help me pls?

like image 600
Juan Carlos Herrera Burbano Avatar asked Oct 19 '25 14:10

Juan Carlos Herrera Burbano


1 Answers

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

vistaConsultaEstadoRUT:formConsultaEstadoRUT:numNit

And : bears a special effect when used within a css-selectors. 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 WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  • Browser Snapshot:

muisca


Reference

You can find a couple of relevant discussions in:

  • Selenium “selenium.common.exceptions.NoSuchElementException” when using Chrome
  • selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element while trying to click Next button with selenium
like image 143
undetected Selenium Avatar answered Oct 21 '25 03:10

undetected Selenium



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!