NoSuchElementException:消息:没有这样的元素:无法使用 Selenium 和 Python 定位元素错误填充用户名

NoSuchElementException: Message: no such element: Unable to locate element error filling user name using Selenium and Python

我想在以下网站输入用户名和密码

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

browser = webdriver.Chrome()
browser.get('https://baud.teamwork.com/launchpad/login?continue=%2Fcrm')
username = driver.find_element_by_id("loginemail")
username.send_keys("YourUsername")

我试过改变

driver.find_element_by_name

仍然无效

我收到以下错误:

NoSuchElementException: Message: no such element: Unable to locate element: {"method":"name","selector":"loginemail"}

尝试改变这个:

username = driver.find_element_by_id("loginemail")

进入这个:

username = browser.find_element_by_id("loginemail")

或整个代码:

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

browser = webdriver.Chrome()
browser.get('https://baud.teamwork.com/launchpad/login?continue=%2Fcrm')
username = browser.find_element_by_id("loginemail")
username.send_keys("YourUsername")

要在 Email address 字段中发送 字符序列 ,您必须引入 for the element_to_be_clickable() and you can use either of the following :

  • 使用CSS_SELECTOR:

    driver.get("https://baud.teamwork.com/launchpad/login?continue=%2Fcrm")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#loginemail"))).send_keys("lucas@whosebug.com")
    
  • 使用XPATH:

    driver.get("https://baud.teamwork.com/launchpad/login?continue=%2Fcrm")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='loginemail']"))).send_keys("lucas@whosebug.com")
    
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  • 浏览器快照:


参考资料

您可以在以下位置找到关于 的一些相关讨论:

Even if your locators correct selenium could not identify , because you need to wait for that element then only you can do some actions 

use this line before username locator line.(but thread sleep does not use nowadays because of time consuming ,if we use thread sleep (3000) our execution script line will delay for 3 sec )

    browser = webdriver.Chrome()
browser.get('https://baud.teamwork.com/launchpad/login?continue=%2Fcrm')
**Thread.sleep(3000);**
username = driver.find_element_by_id("loginemail")
username.send_keys("YourUsername")



Best way is please use Webdriver wait in selenium