Python Selenium上传文件后无法点击上传按钮问题求助
ElementClickInterceptedException:上传按钮无法点击的解决方案
报错信息
Exception has occurred: ElementClickInterceptedException Message: element click intercepted: Element <button data-testid="modal-footer-button-primary" type="button" class="btn btn-primary">...</button> is not clickable at point (945, 650). Other element would receive the click: <div class="modal-footer">...</div>
当前代码
from selenium import webdriver from selenium.webdriver.chrome.service import Service as ChromeService from webdriver_manager.chrome import ChromeDriverManager from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support.select import Select from selenium.webdriver.support import expected_conditions as EC # 阻止Chrome自动关闭 options = webdriver.ChromeOptions() options.add_experimental_option("detach", True) driver=webdriver.Chrome(options=options,service=ChromeService(ChromeDriverManager().install())) driver.maximize_window() driver.get("url") # 点击Clayton State按钮 button1 = driver.find_element(By.CLASS_NAME,"name") button1.click() # 登录Files USG username = driver.find_element(By.NAME, "UserName") username.send_keys('username') password = driver.find_element(By.NAME, "Password") password.send_keys('password') form = driver.find_element(By.ID, "submitButton") form.submit() # USG Duo Push验证 button2 = driver.find_element(By.ID, "DuoAdfsAdapter") button2.click() WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@id='duo_iframe']"))) WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[normalize-space()='Send Me a Push']"))).click() # 选择对应文件夹下拉选项 WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "gotofolderform"))).click() finaid_dd = Select(driver.find_element(By.ID, "field_gotofolder")) finaid_dd.select_by_value("value") # 点击上传按钮打开文件选择窗口 button3 = driver.find_element(By.ID, "fileUploaderButton") button3.click() file_upload = driver.find_element(By.CLASS_NAME, "file-selector-browse") # driver.execute_script("arguments[0].click();", file_upload) --- 暂时不需要 # 输入文件路径 file_input = driver.find_element(By.CLASS_NAME, "file-selector-input") file_path = "filepath" file_input.send_keys(file_path) # 尝试提交上传 file_input.submit()
对应HTML结构

解决方法
1. 用JavaScript直接触发按钮点击
常规点击被modal-footer元素拦截,绕过元素遮挡直接调用按钮的点击事件:
# 替换原代码中的file_input.submit() upload_btn = WebDriverWait(driver, 20).until( EC.element_to_be_clickable((By.DATA_TESTID, "modal-footer-button-primary")) ) driver.execute_script("arguments[0].click();", upload_btn)
2. 确保元素无遮挡且完全加载
先切回主文档(若之前进入过iframe),再滚动到按钮位置后点击:
# 切回主文档 driver.switch_to.default_content() # 等待按钮可见并滚动到视图内 upload_btn = WebDriverWait(driver, 20).until( EC.visibility_of_element_located((By.CLASS_NAME, "btn-primary")) ) driver.execute_script("arguments[0].scrollIntoView(true);", upload_btn) # 执行点击 upload_btn.click()
3. 等待遮罩层消失
如果页面存在加载遮罩,先等待遮罩消失再操作:
# 等待遮罩层隐藏(假设遮罩class为modal-backdrop) WebDriverWait(driver, 20).until( EC.invisibility_of_element_located((By.CLASS_NAME, "modal-backdrop")) ) # 定位并点击上传按钮 upload_btn = WebDriverWait(driver, 20).until( EC.element_to_be_clickable((By.DATA_TESTID, "modal-footer-button-primary")) ) upload_btn.click()
内容的提问来源于stack exchange,提问作者Zyngle
相关产品推荐
相关产品推荐

