2

I'm trying to automate the login process. I am looking for an element with a name but test is fail and the response is "selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method": "css selector", "selector":"[name="emailAddress"] "}" What's wrong with my code?

import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

class MainTests(unittest.TestCase):
   def setUp(self):
       self.driver = webdriver.Chrome(executable_path=r"C:\TestFiles\chromedriver.exe")

   def test_demo_login(self):
       driver = self.driver
       driver.get('http://localhost:8000/login')
       title = driver.title
       print(title)
       assert 'Calculator' == title


       element = driver.find_element_by_name("emailAddress")
       element.send_keys("name123@gmail.com")

       time.sleep(30)
supputuri
  • 13,644
  • 2
  • 21
  • 39
Shibuya_
  • 21
  • 1
  • 1
  • 3

1 Answers1

7

These are the common situations where you will get NoSuchElementException

  1. Locator might be wrong
  2. Element might be there in iframe
  3. Element might be in the other window
  4. 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. enter image description here

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)
supputuri
  • 13,644
  • 2
  • 21
  • 39
  • it turned out that "Element might not loaded by the time script try to find the element" ```Self.driver.implicitly_wait(30)``` did the job. Thank's for your help <3 – Shibuya_ Jul 17 '19 at 07:53
  • If you feel the issue is resolved, please accept the answer by clicking on the hallow check mark below the down vote button on the left hand side. – supputuri Jul 17 '19 at 13:13
  • Why you edited your comments adding the solution I applied, which I wrote about anyway? – Shibuya_ Jul 19 '19 at 09:42
  • people might not read the comments sometime, so I just wanted to make sure the solutions are there in the answer. – supputuri Jul 19 '19 at 13:07