简体   繁体   中英

Selenium NoSuchElementException - Unable to locate element: {“method”:“css selector”,“selector”:“[name=”emailAddress“]”}

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)

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. 在此处输入图片说明

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM