简体   繁体   English

Selenium NoSuchElementException-无法找到元素:{“方法”:“ css选择器”,“选择器”:“ [[名称=”电子邮件地址”]”}

[英]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? 我正在寻找一个具有名称的元素,但测试失败,并且响应为“ selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法找到元素:{“方法”:“ css选择器”,“选择器” :“ [name =” emailAddress“]”}“我的代码有什么问题?

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 这些是常见的情况,您会收到NoSuchElementException

  1. Locator might be wrong 定位器可能有误
  2. Element might be there in iframe 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 1.定位器可能有误

Check if your locator is correct in the browser devtool/console . 浏览器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 2.元素可能在iframe中

Check if the element is present in the iframe rather in the parent document. 检查元素是否存在于iframe中,而不是在父文档中。 在此处输入图片说明

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. 如果您看到该元素在iframe中,则应在找到该元素并与其进行交互之前切换到iframe。 (Remember to switch back to parent document once you are done with the steps on iframe element) (完成iframe元素上的步骤后,请记住切换回父文档)

driver.switch_to.frame("frame_id or frame_name")

You check here for more information. 您可以在此处查看更多信息。

3. Element might be in the other window 3.元素可能在另一个窗口中

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.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 4.时间脚本尝试查找元素时可能未加载该元素

This is the most common reason we see the NoSuchElementException if none of the above is the source of the error. 如果以上都不是错误的根源,这是我们看到NoSuchElementException的最常见原因。 You can handle this with the explicit wait using WebDriverWait as shown below. 您可以使用WebDriverWait通过显式等待来处理此问题,如下所示。

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法找到元素:{“方法”:“css选择器”,“选择器”:“.ui流感~”} - selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".ui flu~"} 无法找到硒中的元素:{“方法”:“ css选择器”,“选择器”:“ [id =” identifierId”]”} - Unable to locate element: {“method”:“css selector”,“selector”:“[id=”identifierId“]”} in selenium NoSuchElementException:消息:没有这样的元素:无法定位元素:{“method”:“css selector”,“selector”:“.selected”} - NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".selected"} NoSuchElementException:消息:没有这样的元素:无法找到元素:{"method":"css selector","selector":"[id="events_table"]"} - NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="events_table"]"} selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法定位元素:{"method":"xpath","selector":""} - selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":""} NoSuchElementException:消息:没有这样的元素:无法定位元素:{“方法”:“名称”,“选择器”:“用户名”},同时向用户名发送文本 - NoSuchElementException: Message: no such element: Unable to locate element: {"method":"name","selector":"username"} while sending text to username 收到消息:没有这样的元素:无法找到元素:{"method":"css selector","selector":"[id="None"]"} Python 使用 selenium 时 - Getting Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="None"]"} Python when using selenium 消息:没有这样的元素:无法定位元素:{“method”:“css selector”,“selector”:“[name=”uID“]”} - Message: no such element: Unable to locate element: {“method”:“css selector”,“selector”:“[name=”uID“]”} 使用 Selenium Python 选择下拉菜单 - 无法定位元素:{“method”:“css selector”,“selector”:"[id= - Selecting drop-down using Selenium Python - Unable to locate element: {“method”:“css selector”,“selector”:"[id= 无法定位元素:{“method”:“css selector”,“selector”:“.Mr508”} - Unable to locate element: {“method”:“css selector”,“selector”:“.Mr508”}
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM