selenium in python : NoSuchElementException: Message: no such element: Unable to locate element

I tried typing 'abc' in the first block of id and 'cdef' in the second block of password. However, the error code at the bottom comes up.

from selenium import webdriver
driver.get('http://sugang.korea.ac.kr')

Added implicitly wait to prevent the code from executing before the page fully loads.

driver.implicitly_wait(30)

Code for adding username and password is as below

driver.find_element_by_name('id').send_keys('abc') driver.find_element_by_name('pw').send_keys('cdef')

But getting below error

NoSuchElementException: Message: no such element: Unable to locate element: {"method":"id","selector":"id"}

please. help me ^^


No Such Element Exception usually comes when web driver can't see the element you are trying to perform an action on. Reason's can be:

  1. your ID or Name or Xpath or CssSelector can be wrong.

  2. Your element might be inside an an iframe so that web driver can't see or detect it. Switch to an iframe through Selenium and python

  3. Your element is taking time to appear on the UI, so you can use an explicit wait to solve this. https://selenium-python.readthedocs.io/waits.html

The username and password fields are within an frame, so you have to:

  • Induce WebDriverWait for the desired frame to be available and switch to it.
  • Induce WebDriverWait for the desired element to be clickable.
  • You can use the following solution:

    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By
    
    driver = webdriver.Firefox(executable_path=r'C:\\Utility\\BrowserDrivers\\geckodriver.exe')
    driver.get("http://sugang.korea.ac.kr")
    WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.NAME,"firstF")))
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.input_login[name='id']"))).send_keys('abc')
    driver.find_element_by_css_selector("input.input_login[name='pw']").send_keys("cdef")
    
  • Browser Snapshot:

sugang_korea_ac_kr


Add explicitly wait

from selenium.webdriver.support import expected_conditions as EC

userNameElement= WebDriverWait(driver, 2).until(
EC.presence_of_element_located((By.NAME, "id"))
userNameElement.send_keys('abc')

pwdElement= WebDriverWait(driver, 2).until(
EC.presence_of_element_located((By.NAME, "pwd"))
pwdElement.send_keys('cdef')

Here, I am expecting that your locators are correct.


It is in a frame which you need to switch to first. Also, use ids where possible as they are faster.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

url ="http://sugang.korea.ac.kr"
driver = webdriver.Chrome()
driver.get(url)
WebDriverWait(driver,5).until(EC.visibility_of_element_located((By.CSS_SELECTOR,'[name=firstF]')))
driver.switch_to.frame(driver.find_element_by_css_selector('[name=firstF]'))
WebDriverWait(driver,5).until(EC.visibility_of_element_located((By.ID,'id'))).send_keys('abc')
driver.find_element_by_id('pw').send_keys('def')
driver.find_element_by_id('loginButton').click()