简体   繁体   English

尝试使用 selenium 登录我大学的网页时,我不断收到错误消息 NoSuchElementException

[英]I keep getting the error message NoSuchElementException when trying to use selenium to log into my university's webpage

I'm pretty new to python and StackOverflow so please bear with me.我对 python 和 StackOverflow 还很陌生,所以请耐心等待。 I'm trying to write a script in python and use selenium to log myself into my university's website but I keep getting the same error NoSuchElementException.我正在尝试用 python 编写脚本并使用 selenium 将自己登录到我大学的网站,但我一直收到相同的错误 NoSuchElementException。

The full text of the error:错误的全文:

Exception has occurred: NoSuchElementException
Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="username"]"}
  (Session info: chrome=86.0.4240.183)
  File "C:\Users\User\Desktop\Python\Assignment6\nsuokSelenium.py", line 9, in <module>
    browser.find_element_by_id('username').send_keys(bb_username)

I have my log in information in a separate script called credential.py that I'm calling with我在一个名为 credential.py 的单独脚本中包含我的登录信息,我正在调用该脚本

from credentials import bb_username, bb_password

My Code我的代码

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC 
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
from credentials import bb_password, bb_username
browser = webdriver.Chrome()
browser.get('https://bb.nsuok.edu')
browser.find_element_by_id('username').send_keys(bb_username)
browser.find_element_by_id('password').send_keys(bb_password)
browser.find_element_by_name('submit').click()
try:
    WebDriverWait(browser, 1) .until(EC.url_matches('https://bb.nsuok.edu/ultra'))
except TimeoutError:
    print('took too long')
WebDriverWait(browser, 10).until(EC.url_matches('https://bb.nsuok.edu/ultra')) 
browser.find_element_by_name('Courses').click()
WebDriverWait(browser, 10).until(EC.url_matches('https://bb.nsuok.edu/ultra/course')) 
browser.find_element_by_name('Organizations').click()
WebDriverWait(browser, 10).until(EC.url_matches('https://bb.nsuok.edu/ultra/logout')) 

The error is showing up here错误出现在这里

browser.find_element_by_id('username').send_keys(bb_username)

Could it be an issue with PATH?会不会是 PATH 的问题?

You may need to wait for the element.您可能需要等待元素。 Try something like the following:尝试类似以下内容:

element = WebDriverWait(browser, 10).until(
    EC.presence_of_element_located((By.ID, "username"))
    )
element.clear()
element.send_keys(bb_username)

element = WebDriverWait(browser, 10).until(
    EC.presence_of_element_located((By.ID, "password"))
    )
element.clear()
element.send_keys(bb_password)

What Justin Ezequiel said is correct. Justin Ezequiel 说的是正确的。 You need to add waits in your code for the page to load properly;您需要在代码中添加等待页面才能正确加载; due to the fact that, dependent upon internet speeds, some pages load faster than others.由于以下事实,取决于互联网速度,某些页面的加载速度比其他页面快。 ( obviously ) ( 明显地 )

With that in mind, I was able to identify the elements on the page for you.考虑到这一点,我能够为您识别页面上的elements I added some comments in the code as well.我也在代码中添加了一些注释。

MAIN PROGRAM - For Reference主程序 - 供参考

from selenium import webdriver
from selenium.webdriver.chrome.webdriver import WebDriver as ChromeDriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as DriverWait
from selenium.webdriver.support import expected_conditions as DriverConditions
from selenium.common.exceptions import WebDriverException


def get_chrome_driver():
    """This sets up our Chrome Driver and returns it as an object"""
    path_to_chrome = "F:\Selenium_Drivers\Windows_Chrome85_Driver\chromedriver.exe"
    chrome_options = webdriver.ChromeOptions() 
    
    # Browser is displayed in a custom window size
    chrome_options.add_argument("window-size=1500,1000")
    
    return webdriver.Chrome(executable_path = path_to_chrome,
                            options = chrome_options)

    
def wait_displayed(driver : ChromeDriver, xpath: str, int = 5):
        try:
            DriverWait(driver, int).until(
                DriverConditions.presence_of_element_located(locator = (By.XPATH, xpath))
            )
        except:
            raise WebDriverException(f'Timeout: Failed to find {xpath}')
  
        
def enter_information(driver : ChromeDriver, xpath: str, text : str):
    driver.find_element(By.XPATH, xpath).send_keys(text)
    if(driver.find_element(By.XPATH, xpath).get_attribute('value').__len__() != text.__len__()):
        raise Exception(f'Failed to populate our Textbox.\nXPATH: {xpath}')


# Gets our chrome driver and opens our site
chrome_driver = get_chrome_driver()
chrome_driver.get("https://logon.nsuok.edu/cas/login")

# Waits until our elements are loaded onto the page
wait_displayed(chrome_driver, "//form//input[@id='username']")
wait_displayed(chrome_driver, "//form//input[@id='password']")
wait_displayed(chrome_driver, "//form//input[contains(@class, 'btn-submit')]")

# Inputs our Username and Password
enter_information(chrome_driver, "//form//input[@id='username']", "MyUserNameHere")
enter_information(chrome_driver, "//form//input[@id='password']", "MyPasswordHere")

# Clicks Login
chrome_driver.find_element(By.XPATH, "//form//input[contains(@class, 'btn-submit')]").click()

chrome_driver.quit()
chrome_driver.service.stop()

暂无
暂无

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

相关问题 尝试使用Selenium Python时出现NoSuchElementException - NoSuchElementException when trying to use Selenium Python 尝试使用I / O时不断出现属性错误 - Keep getting attribute error when trying to use I/O 即使元素确实存在,我仍然在 selenium 中收到 NoSuchElementException - I keep getting a NoSuchElementException in selenium even though the element DOES exist Python Selenium - NoSuchElementException。 为什么我一直收到这个? - Python Selenium - NoSuchElementException. Why do I keep getting this? 我正在尝试使用 Facebook API 获取有关广告的潜在客户信息,但我不断收到错误消息 - I'm trying to use the Facebook API to get lead information on an ad, but I keep getting an error message 尝试在网页中定位元素但出现 NoSuchElementException - Trying to locate an element in a webpage but getting NoSuchElementException 尝试使用 selenium 打开网页但出现错误 - Trying to open a webpage using selenium but getting error 我正在尝试从网页中抓取一些数据并不断收到 selenium.common.exceptions.TimeoutException 错误 - I'm trying to scrape some data from a webpage and keep getting selenium.common.exceptions.TimeoutException errors 硒蟒蛇 | 选择存在的元素时出错(NoSuchElementException) - SELENIUM PYTHON | Getting error when selecting an element which exists (NoSuchElementException) 为什么我收到此错误? selenium.common.exceptions.NoSuchElementException - Why i am getting this error ? selenium.common.exceptions.NoSuchElementException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM