0

I'm trying to access a city hall portal using Selenium, but I'm having difficulties filling in the login details. I'm trying to use XPath as a way to locate the elements, but I'm not having success. The website I'm working with is: https://directa.natal.rn.gov.br/open.do?sys=DIR&a=wqf45tfes

I'm trying to fill in the username and password fields. I would appreciate any tips on how to proceed because the usual XPath method that works on other websites is not successful in this case.

I used the following attempt:

usuario = browser.find_element(By.XPATH,'//*[@id="usuario"]')
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    What exactly does "not successful" mean? If you're getting an error, or unexpected results, you have to **show them to us**. Otherwise we are just guessing at the actual problem. – John Gordon Jun 18 '23 at 15:13
  • Also, show us the html code for the element on the page that you expected to find. – John Gordon Jun 18 '23 at 15:13
  • Also, since you know the item id, why bother using xpath? `find_element(By.ID, "usuario")` is much more direct. – John Gordon Jun 18 '23 at 15:14

2 Answers2

0

You can use this Xpath for selecting username:

//input[@id='usuario']
usuario = browser.find_element(By.XPATH,'//input[@id='usuario']')

for password field:

//input[@id='senha']
senha = browser.find_element(By.XPATH,'//input[@id='senha']')

for Submit button:

//button[@id='acessar']
acessar= browser.find_element(By.XPATH,'//button[@id='acessar']')

you should first send the username and password and then click the acessar button. hope it helps!

0

To send a character sequence to the username and password field as the elements are within an <iframe> so you have to:

  • Induce WebDriverWait for the frame_to_be_available_and_switch_to_it.

  • Induce WebDriverWait for the desired element to be clickable.

  • You can use either of the following locator strategies:

    • Using CSS_SELECTOR:

      driver.get("https://directa.natal.rn.gov.br/open.do?sys=DIR&a=qxG6rejnY45ft")
      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"frame[name='mainsystem']")))
      WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#usuario"))).send_keys("João Felipe")
      driver.find_element(By.CSS_SELECTOR, "input#senha").send_keys("João Felipe")
      
    • Using XPATH:

      driver.get("https://directa.natal.rn.gov.br/open.do?sys=DIR&a=qxG6rejnY45ft")
      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//frame[@name='mainsystem']")))
      WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='usuario']"))).send_keys("João Felipe")
      driver.find_element(By.XPATH, "//input[@id='senha']").send_keys("João Felipe")
      
  • 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:

Felipe


Reference

You can find a couple of relevant discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352