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

Python Selenium操作Zeppelin删除笔记本确认按钮点击无效求助

Python Selenium自动化删除Zeppelin笔记本失败问题

手动操作可正常删除Zeppelin笔记本(账号拥有完整权限),但通过Python Selenium自动化执行时,尽管代码显示已点击确认对话框的OK按钮,笔记本始终未被移入回收站。

登录及触发删除的代码

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time as TM

service = Service()
options = webdriver.ChromeOptions()
control = webdriver.Chrome(service=service, options=options)
control.implicitly_wait(10) #Cuando algo no aparece, prueba cada 1 seg. durante 20 seg. 
control.delete_all_cookies()

#WEB - LOGIN
url_del = "http://zeppelin_server:1919/#/notebook/2F7T7WPZP"
control.get('http://zeppelin_server:1919/#/')
wait = WebDriverWait(control, 10)

# Go to button
element = control.find_element(By.CSS_SELECTOR, '[ng-click="navbar.showLoginWindow()"]')

#Clicking on the button to be selected
element.click()
wait = WebDriverWait(control, 30)
username_field = wait.until(EC.visibility_of_element_located((By.ID, 'userName')))
password_field = wait.until(EC.visibility_of_element_located((By.ID, 'password')))
login_button = control.find_element(By.CSS_SELECTOR, 'button.btn.btn-default.btn-primary')

#Perform actions on the displayed elements
username_field.send_keys('user')
password_field.send_keys('password')

#Click on the button
login_button.click()
cookies = control.get_cookies()
wait = WebDriverWait(control, 10)

TM.sleep(5)
#Handling Cookies
for cookie in cookies:
    control.add_cookie(cookie)
control.get(url_del)    

TM.sleep(10)

# Find the button element by its class name or CSS selector
#button_element = control.find_element(By.CSS_SELECTOR, "button.btn.btn-default.btn-xs.ng-scope")
button_element = control.find_element(By.CSS_SELECTOR, 'button[ng-click="moveNoteToTrash(note.id)"]')

# Click the button
button_element.click()

确认对话框的HTML结构

<div class="modal bootstrap-dialog type-primary size-normal in" role="dialog" aria-hidden="true" id="49e1dec0-f885-4c33-9693-86a8d847ccc0" aria-labelledby="49e1dec0-f885-4c33-9693-86a8d847ccc0_title" tabindex="-1" style="display: block; padding-right: 17px; z-index: 1050;">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <div class="bootstrap-dialog-header">
                    <div class="bootstrap-dialog-close-button">
                        <button class="close" aria-label="close">×</button>
                    </div>
                    <div class="bootstrap-dialog-title" id="49e1dec0-f885-4c33-9693-86a8d847ccc0_title">Move this note to trash?</div>
                </div>
            </div>
            <div class="modal-body">
                <div class="bootstrap-dialog-body">
                    <div class="bootstrap-dialog-message">This note will be moved to <strong>trash</strong>.</div>
                </div>
            </div>
            <div class="modal-footer">
                <div class="bootstrap-dialog-footer">
                    <div class="bootstrap-dialog-footer-buttons">
                        <button class="btn btn-default" id="3124e67c-a4b2-42c1-859d-71c9140ff485">Cancel</button>
                        <button class="btn btn-primary" id="9f9cea0d-b364-4ff5-85eb-02ea143e8fb2">OK</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

处理确认按钮的代码

wait = WebDriverWait(control, 10)
ok_button = WebDriverWait(control, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '.bootstrap-dialog-footer-buttons button.btn-primary')))
TM.sleep(10)
# Click the "OK" button
ok_button.click()

TM.sleep(10)
control.quit()

解决建议

  • 移除所有TM.sleep()硬等待,替换为显式等待。比如先等待确认对话框标题可见,确保对话框完全渲染后再操作按钮:
    # 等待对话框标题出现
    wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, '.bootstrap-dialog-title')))
    # 再等待按钮可点击
    ok_button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '.bootstrap-dialog-footer-buttons button.btn-primary')))
    ok_button.click()
    
  • 尝试用ActionChains模拟真实用户点击动作,避免元素点击未触发绑定事件:
    from selenium.webdriver.common.action_chains import ActionChains
    
    action = ActionChains(control)
    action.move_to_element(ok_button).click().perform()
    
  • 如果元素点击无效,改用JavaScript执行点击:
    control.execute_script("arguments[0].click();", ok_button)
    
  • 检查页面是否存在iframe嵌套,确认对话框是否在iframe内。如果是,需要先切换到对应iframe再操作:
    # 假设对话框在某个iframe中,先定位并切换
    iframe = wait.until(EC.presence_of_element_located((By.TAG_NAME, 'iframe')))
    control.switch_to.frame(iframe)
    # 操作按钮...
    # 操作完成切回主文档
    control.switch_to.default_content()
    
  • 点击OK后,添加验证步骤:等待目标笔记本从列表中消失,或者检查回收站是否出现该笔记本,确认操作是否成功。

内容的提问来源于stack exchange,提问作者alejomarchan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.15 08:54:56