簡體   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