【问题标题】:selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element error using Selenium through Pythonselenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法通过 Python 使用 Selenium 定位元素错误
【发布时间】:2020-05-03 23:37:54
【问题描述】:

我正在尝试使用 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 表单控制"}

这是我的网站,所以我确信这门课,我看过 SO,答案是关于 iframe,我没有。

我已经尝试过使用类、id 等。除了没有填充输入之外,一切正常。

【问题讨论】:

标签: python selenium xpath css-selectors webdriverwait


【解决方案1】:

此错误消息...

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

...暗示 ChromeDriver 无法在 Browsing ContextChrome 浏览器 会话中找到所需的元素。 p>


您需要注意以下几点:


要在 Identifiant 字段中发送 字符序列,您必须为 element_to_be_clickable() 诱导 WebDriverWait,您可以使用关注Locator Strategies

  • 使用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;博士

您可以在Selenium “selenium.common.exceptions.NoSuchElementException” when using Chrome

中找到关于NoSuchElementException的相关详细讨论

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 嗯,我不知道你要去哪里......它是有效的,selenium 只是在寻找 id 或标签......我设法通过调用“名称”使其工作筛选。我必须说,你的回答没有用。
    • 尝试将 html 放入 xml 验证器,您会明白我的意思是说它由于未封闭的标签而无效。这不再重要,因为您已经解决了您的问题。
    猜你喜欢
    • 1970-01-01
    • 2019-10-25
    • 1970-01-01
    • 1970-01-01
    • 2022-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多