Solve – Selenium in python – Message: no such element: Unable to locate element In Python

Selenium in python – Message: no such element: Unable to locate element is an error that occurs in Python when the web driver can not see the element we are trying to manipulate.

In this article, I am going to show you how to replicate this error and solve it, I am sharing the solution which worked for me and even the reason why the error is occurring in the first place, I am also sharing other solutions which worked for other developers.

Describing Error : Selenium in python – Message: no such element: Unable to locate element

Let us try to replicate this error and try to explain why it is happening in the first place.

An example of the occurrence of this problem is the case bellow.

We are building an automation solution using Selenium, we are trying to add “abc’ and “cdef” consecutively to the first block called id and second block called password. The two lines bellow are how we are trying to achieve that:

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

Before that we started our code this way

                                                                       #
from selenium import webdriver
driver.get('http://twitter.com')
                                                                       #

Depending on your code the situation is going to be different to an extent but very similar.

The error occurs because the web driver can not see the element we are trying to manipulate.

Solution 1 : switch to an iframe in Selenium

The reasons why the web driver can not see the element are multiple, depending on your case the situation is different, for example if your element is inside an iframe there is no way for the web driver to get it.

In this case you should learn how you can switch to an iframe in Selenium, the process is easy and a quick google search is enough.

This code solve the issue by first switching to the iframe, please adapt this solution to your needs.

                                                                       #
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()
                                                                       #

Solution 2 : uses WebDriverWait in Selenium

Another very popular reason is that your element does not have enough time to load in the UI, so you should insert waits in your Python code. Again this article is not about waits but the syntax is very simple and easy.

Here is an example which uses WebDriverWait

                                                                       #
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")
                                                                       #

Solution 3 : use the correct Xpath , Css Selectors or Class ids in Selenium

Another reason is that Xpath is wrong or other alternatives like Css Selectors or Class ids are wrong.

I hope these solutions worked for you, please consider donating to our Kofi account, you can use the red button on top of this page.

Summing-up

The article is over, I hope I have been able to help you solve this error or at least guide you in the right direction, check out other solutions to different errors you can do that by using the search bar on top of this page.

Keep learning, keep coding guys, cheers.

If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/