Search code examples
pythonseleniumselenium-webdriverframewebdriverwait

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')

I added an implicit 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 I am getting the below error:

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


Solution

  • 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