Python 中的 Selenium 错误 - 消息:没有这样的元素:无法找到元素

Selenium Error in Python - Message: no such element: Unable to locate element

我正在尝试使用 Selenium 从受密码保护的网站获取数据。 但是,我在登录时一开始就卡住了,并显示以下错误消息:

NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[name="username"]"}
  (Session info: chrome=87.0.4280.66)

我使用的名称肯定是正确的,我检查了该网站。包括等待时间也无济于事...

我的代码是:

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



driver = webdriver.Chrome()


driver.get("https://clearing.apcs.at/emwebapcsem/startApp.do")

print(driver.title)
print(driver.current_url)


# create an object for searchbox
username=driver.find_element_by_name("username")
password=driver.find_element_by_name("password")
# typte the input
username.send_keys("XXXXXX")
password.send_keys("XXXXXX")
driver.find_element_by_name('login').click()

如有任何建议,我们将不胜感激。

将字符序列发送到 BenutzerPasswort 字段,因为元素在 <frame> 中,所以您有至:

  • 诱导 WebDriverWait 所需的 帧可用并切换到它.

  • 诱导 所需的 元素可点击

  • 您可以使用以下任一项:

    • 使用CSS_SELECTOR:

      driver.get('https://clearing.apcs.at/emwebapcsem/startApp.do')
      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"frame[title='menu']")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.loginContentBoxInput[name='username']"))).send_keys("Endre")
      driver.find_element_by_css_selector("input.loginContentBoxInput[name='password']").send_keys("Endre")
      
    • 使用XPATH:

      driver.get('https://clearing.apcs.at/emwebapcsem/startApp.do')
      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//frame[@title='menu']")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='loginContentBoxInput' and @name='username']"))).send_keys("Endre")
      driver.find_element_by_xpath("//input[@class='loginContentBoxInput' and @name='password']").send_keys("Endre")
      
  • 注意:您必须添加以下导入:

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


参考

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

  • Switch to an iframe through Selenium and python