You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Selenium(Webdriver)无法找到元素问题求助

Selenium登录FAP系统定位元素失败问题解决

用户代码

import requests
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from time import sleep
import os


login_page = "https://fap.fpt.edu.vn/Default.aspx"
# page = "https://fap.fpt.edu.vn/Report/ScheduleOfWeek.aspx"

email = ""
password = ""

options = Options()
options.add_argument("--window-size=1920,1080")
options.binary_location = "C:\Program Files\Google\Chrome\Application\chrome.exe"
driver = webdriver.Chrome(options = options)
driver.get(login_page)

login1 = driver.find_element("xpath","//div[@class='abcRioButtonContentWrapper']").click()


driver.find_element(By.NAME, "identifier").send_keys(email)
sleep(3)
driver.find_element(By.ID, "identifierNext").click()
sleep(2)
driver.find_element(By.NAME, "password").send_keys(password)
sleep(2)
driver.find_element(By.ID, "passwordNext").click()


sleep(999999)

问题描述

我在使用Selenium自动化登录FAP系统时,点击Google登录按钮后,认为选择了正确的By.NAME("identifier")定位邮箱输入框,但代码运行失败,返回“no such element”提示。我尝试修改语法、改用Xpath或By.ID定位元素,问题仍未解决。

解决方案

核心原因

Google登录弹窗是嵌入在iframe中的,Selenium默认只会在当前主页面的DOM树中查找元素,无法直接访问iframe内部的元素,这才是定位失败的关键。

修正步骤

  1. 点击Google登录按钮后,等待目标iframe加载完成。
  2. 将Selenium的上下文切换到该iframe内,之后才能正常定位里面的输入框和按钮。
  3. 操作完成后,若需回到主页面,可切换回默认上下文。

修改后的代码

import requests
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from time import sleep
import os


login_page = "https://fap.fpt.edu.vn/Default.aspx"
# page = "https://fap.fpt.edu.vn/Report/ScheduleOfWeek.aspx"

email = ""
password = ""

options = Options()
options.add_argument("--window-size=1920,1080")
options.binary_location = "C:\Program Files\Google\Chrome\Application\chrome.exe"
driver = webdriver.Chrome(options = options)
driver.get(login_page)

# 点击Google登录按钮
driver.find_element("xpath","//div[@class='abcRioButtonContentWrapper']").click()

# 等待Google登录的iframe加载并切换上下文
wait = WebDriverWait(driver, 10)
iframe = wait.until(EC.presence_of_element_located((By.XPATH, "//iframe[contains(@src, 'accounts.google.com')]")))
driver.switch_to.frame(iframe)

# 定位邮箱输入框并输入
wait.until(EC.presence_of_element_located((By.NAME, "identifier"))).send_keys(email)
# 点击下一步按钮
wait.until(EC.element_to_be_clickable((By.ID, "identifierNext"))).click()

# 等待密码输入框加载并输入
wait.until(EC.presence_of_element_located((By.NAME, "password"))).send_keys(password)
# 点击登录按钮
wait.until(EC.element_to_be_clickable((By.ID, "passwordNext"))).click()

# 若后续需要操作主页面元素,切换回默认上下文
# driver.switch_to.default_content()

sleep(999999)

额外优化说明

  • 用WebDriverWait替代固定sleep:根据元素的实际加载状态等待,避免因网络延迟或页面加载慢导致的元素未就绪问题,提升代码稳定性。
  • 动态定位iframe:通过contains(@src, 'accounts.google.com')定位iframe,比固定ID更可靠,避免Google登录页面的iframe ID动态变化。

内容的提问来源于stack exchange,提问作者Nguyen Huu Phuc

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 04:20:55