Solve error “selenium.common.exceptions.NoSuchElementException” when using Chrome

selenium.common.exceptions.NoSuchElementException is a known error in Python which occurs when we use the webdriver in Python.

In this article I am going to show why this error is happening and how we can solve it with multiple solutions, hopefully one of them is enough to solve the problem.

Describing “selenium.common.exceptions.NoSuchElementException” when using Chrome

This error is one of the most popular errors in Python and happens for many reasons that are very different, the error is known and is solvable with a couple of possible solutions. And that is even if the reason it happens is rare.

In this example the error occurs in Selenium while trying to run a simple video game on the chrome browser.

Let us try to replicate the error by using the code bellow.

First we will start with importing the webdriver.

                                                                       #
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time
                                                                       #

Now we will open the page we want to access. In this case the page is : http://www.foddy.net/Athletics.html?webgl=true

                                                                       #
browser = webdriver.Chrome()
browser.set_window_size(640, 480)
browser.get('http://www.foddy.net/Athletics.html?webgl=true')
browser.implicitly_wait(10)
                                                                       #

Now let us select the element we want to get and click it

                                                                       #
canvas = browser.find_element_by_id("window1")
canvas.click()

while (True):
    action = ActionChains(browser)
    action.move_to_element(canvas).perform()
    canvas.click()
    canvas.send_keys("q")
                                                                       #

The code above belongs in order to the same .py file. Everything should work fine but still we get the error bellow.

                                                                       #
selenium.common.exceptions.NoSuchElementException: 
Message: no such element: Unable to locate element
"method":"id","selector":"window1"
                                                                       #

Solution : set the viewport to the default one in Selenium. Or Get an element from inside an iframe

First of all the Exception happens when we are trying to select the element from the page we have just Loaded using Selenium.

We are either using a normal selector or an Xpath

This is an example of the line of code where the error happens.

                                                                       #
my_element = driver.find_element_by_xpath("xpath_expression")
                                                                       #

One reason the error can occur is if the code cannot find the element we are trying to select inside the DOM.

In this case the solution is easy, make sure you are using the correct selector.

Sometimes the selector we are using is correct, but the viewport is not set correctly. This means the selector is not inside the viewport, a quick fix is set the viewport to the default one.

Another possible reason for the error is if the element we are trying to get is located inside an iframe, that explains why we have the error since we cannot access the element directly with our selector.

In this case adapting the solution bellow will do the job. If there are many iframes in the page, you can select the index of the one you want.

                                                                       #
driver.switch_to.frame(1)
                                                                       #

If the index does not do the job you can try using the name of the iframe

                                                                       #
driver.switch_to.frame("iframe_name")
                                                                       #

If that does not work try using the id of the iframe instead.

                                                                       #
driver.switch_to.frame("iframe_id")
                                                                       #

Finally, another popular reason is if you are using the correct selector but the element you are trying to select is using the style attribute as follows.

                                                                       #
style="display: none;"
                                                                       #

The solution in this case is to remove the attribute using executeScript().

                                                                       #
elem = driver.find_element_by_xpath("element_xpath")
driver.execute_script("arguments[0].removeAttribute('style')", elem)
                                                                       #

Then use the element however you want.

Summing-up

This is the end of this article, I hope we helped you solve the error or at least show you why it happens and guided you in the right direction.

If you want to help, you can donate to our Kofi account using the red button at the top of this page. Keep learning, keep coding and cheers.

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