These are the common situations where you will get NoSuchElementException
- Locator might be wrong
- Element might be there in iframe
- Element might be in the other window
- Element might not loaded by the time script try to find the element
Now, let's see how to handle each of these situations.
1. Locator might be wrong
Check if your locator is correct in the browser devtool/console.
If the locator is not correct in your script, update the locator. If it's correct, then move to the next step below.
2. Element might be there in iframe
Check if the element is present in the iframe rather in the parent document.

If you see the element is in the iframe then you should switch to the iframe, before finding the element and interact with the same. (Remember to switch back to parent document once you are done with the steps on iframe element)
driver.switch_to.frame("frame_id or frame_name")
You check here for more information.
3. Element might be in the other window
Check if the element is present in a new tab/window. If that's the case then you have to switch to the tab/window using switch_to.window
.
# switch to the latest window
driver.switch_to.window(driver.window_handles[-1])
# perform the operations
# switch back to parent window
driver.switch_to.window(driver.window_handles[0])
4. Element might not loaded by the time script try to find the element
This is the most common reason we see the NoSuchElementException if none of the above is the source of the error. You can handle this with the explicit wait using WebDriverWait
as shown below.
You need the below imports to work with the explicit wait.
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Script:
# lets say the "//input[@name='q']" is the xpath of the element
element = WebDriverWait(driver,30).until(EC.presence_of_element_located((By.XPATH,"//input[@name='q']")))
# now script will wait unit the element is present max of 30 sec
# you can perform the operation either using the element returned in above step or normal find_element strategy
element.send_keys("I am on the page now")
You can also use implicit wait as shown below.
driver.implicitly_wait(30)