3

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?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Can you try escaping the special chars in the name using css `[name="vistaConsultaEstadoRUT\:formConsultaEstadoRUT\:numNit"]` or just send the name with escaped chars `"vistaConsultaEstadoRUT\:formConsultaEstadoRUT\:numNit"` – supputuri Jun 01 '20 at 04:08
  • Scaping the chars with backslash didn't work. And using css gave me 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)` Thanks in advance for your help Code: `driver.find_element_by_css_selector("vistaConsultaEstadoRUT\:formConsultaEstadoRUT\:numNit")` – Juan Carlos Herrera Burbano Jun 01 '20 at 04:25

1 Answers1

2

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 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:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    Neither worked. Thanks for your answer btw @DebanjanB. The error still appears: -using `css_selector`, error: `NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"input[name='vistaConsultaEstadoRUT:formConsultaEstadoRUT:numNit']"} (Session info: chrome=81.0.4044.138)` -using `xpath`, error: `NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//input[@name='vistaConsultaEstadoRUT:formConsultaEstadoRUT:numNit']"} (Session info: chrome=81.0.4044.138)` – Juan Carlos Herrera Burbano Jun 01 '20 at 12:26
  • @JuanCarlosHerreraBurbano Check out the update answer and let me know the status. – undetected Selenium Jun 01 '20 at 12:50
  • @DebanjaB sure! I am so sorry to ask you this but I got stuck again. No worries if you don't answer, I understand is another question. I try to copy an element but it just doesn't make it. `element_to_copy = WebDriverWait(driver, 20).until(EC.element_to_be_selected((By.CSS_SELECTOR, "input[class='tipoFilaNormalVerde']"))) print(element_to_copy.text)` – Juan Carlos Herrera Burbano Jun 01 '20 at 14:32
  • @JuanCarlosHerreraBurbano This error may be unique and different from what you have asked within this question. If these answers do not fully address your question, please [ask a new question](https://stackoverflow.com/questions/ask). – undetected Selenium Jun 01 '20 at 14:34