繁体   English   中英

selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法通过 Python 使用 Selenium 定位元素错误

[英]selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element error using Selenium through Python

我正在尝试使用 selenium 在我的网站上自动登录。

我正在访问这个网站: http : //front-desk.fr/users/accounts/login/?next=/

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

chrome_path = r"C:\Users\gaeta\OneDrive\Bureau\chromedriver.exe"

class FrontDesk:
    def __init__(self,username,password):
        self.username = username
        self.password = password
        self.bot = webdriver.Chrome(chrome_path)

    def login(self):
        bot = self.bot
        bot.get("http://front-desk.fr/users/accounts/login/")
        time.sleep(5)
        email = bot.find_element_by_class_name('textinput textInput form-control')
        email.send_keys(self.username)



ed = FrontDesk('myemail@gmail.com', 'password1234')
ed.login()

但是出现错误:

selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法定位元素:{"method":"css selector","selector":".textinput textInput form-control"}

这是我的网站,所以我确定课程,我看过 SO,答案是关于 Iframe,我没有。

我已经尝试过使用 class、id 等。除了它不填充输入外,一切正常。

这个错误信息...

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".textinput textInput form-control"}

...暗示ChromeDriver无法在浏览上下文(Chrome 浏览器会话)中找到所需的元素。


您需要注意以下几点:


要在Identifiant字段中发送字符序列,您必须为element_to_be_clickable()引入WebDriverWait ,您可以使用以下任一定位器策略

  • 使用CSS_SELECTOR

     WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#id_login"))).send_keys(self.username)
  • 使用XPATH

     WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='id_login']"))).send_keys(self.username)
  • 注意:您必须添加以下导入:

     from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC

tl; 博士

在使用 Chrome 时,您可以在Selenium “selenium.common.exceptions.NoSuchElementException”中找到有关NoSuchElementException的相关详细讨论

HTML 源代码无效,有很多未关闭的标签,例如用于样式表的标签。 修复这些问题,然后重试。

暂无
暂无

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

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