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

Selenium批量处理工单时循环执行出现点击拦截错误求助

批量处理工单时点击被遮罩拦截的解决方案

问题根源

错误提示里的<div id="wait">是页面的全局加载遮罩,批量循环处理工单时,切换工单后页面仍在加载(遮罩未消失),代码就尝试点击元素,导致点击被遮罩拦截。目前代码只处理了msgbox-dialog_inner_dialogwait对话框遮罩,未处理全局的wait遮罩,这是随机报错的核心原因。

修复步骤

1. 添加等待全局遮罩消失的通用函数

新增复用的等待函数,统一处理全局加载遮罩和对话框遮罩:

def wait_for_global_load(driver, timeout):
    # 等待全局加载遮罩消失
    WebDriverWait(driver, timeout).until(EC.invisibility_of_element((By.ID, "wait")))
    # 同时等待对话框遮罩消失
    WebDriverWait(driver, timeout).until(EC.invisibility_of_element((By.ID, "msgbox-dialog_inner_dialogwait")))

2. 在关键操作节点调用等待函数

在工单搜索、点击操作前后调用该函数,确保页面完全加载后再执行下一步:

  • 搜索工单回车后调用
  • 点击优先级按钮前调用
  • 保存操作后调用,确保页面回到可操作状态

3. 修复iframe输入逻辑

直接对iframe元素send_keys无效,需先切换到iframe内部,找到输入区域再输入:

# 切换到iframe
new_log_text_iframe = WebDriverWait(driver, timeout).until(
    EC.frame_to_be_available_and_switch_to_it((By.ID, "mfc77772d-rte_iframe"))
)
# 找到iframe内的输入区域(通常是body标签)
new_log_text_id = WebDriverWait(driver, timeout).until(EC.element_to_be_clickable((By.TAG_NAME, "body")))
new_log_text_id.send_keys(log_text)
# 操作完成后切回主文档
driver.switch_to.default_content()

修改后的完整代码

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

email = 'ryan.hills@company.com'
pwd = 'password123'

incidents_list = ["inc_0001", "inc_0002", "inc_0003", "inc_0004", "inc_0005", "inc_0006", "inc_0007", "inc_0008", "inc_0009"]

incident_ticket = 'inc_0010'
log_title = 'Note'
log_text = "Ticket it's beign resolved as low priority case, approved by PM."

timeout = 20

def wait_for_global_load(driver, timeout):
    # 等待全局加载遮罩和对话框遮罩消失
    WebDriverWait(driver, timeout).until(EC.invisibility_of_element((By.ID, "wait")))
    WebDriverWait(driver, timeout).until(EC.invisibility_of_element((By.ID, "msgbox-dialog_inner_dialogwait")))

def ticket_processing(driver, incident):
    try:
        wait_for_global_load(driver, timeout)
        
        quicksearch_id = WebDriverWait(driver, timeout).until(EC.element_to_be_clickable((By.ID, "quicksearch")))
        quicksearch_id.clear()
        quicksearch_id.send_keys(incident)
        quicksearch_id.send_keys(Keys.ENTER)

        # 搜索后等待页面加载完成
        wait_for_global_load(driver, timeout)
        
        urgency_id = WebDriverWait(driver, timeout).until(EC.element_to_be_clickable((By.ID, "m45bfddac-img")))
        urgency_id.click()
        
        wait_for_global_load(driver, timeout)
        p3_selection_id = WebDriverWait(driver, timeout).until(
            EC.element_to_be_clickable((By.ID, "lookup_page1_tdrow_[C:1]_ttxt-lb[R:2]")))
        p3_selection_id.click()

        wait_for_global_load(driver, timeout)
        new_log_btn_id = WebDriverWait(driver, timeout).until(
            EC.element_to_be_clickable((By.ID, "ma877f29e_bg_button_addrow-pb")))
        new_log_btn_id.click()

        wait_for_global_load(driver, timeout)
        new_log_title_id = WebDriverWait(driver, timeout).until(EC.element_to_be_clickable((By.ID, "m8b7047bb-tb")))
        new_log_title_id.send_keys(log_title)

        # 处理iframe输入
        wait_for_global_load(driver, timeout)
        new_log_text_iframe = WebDriverWait(driver, timeout).until(
            EC.frame_to_be_available_and_switch_to_it((By.ID, "mfc77772d-rte_iframe")))
        new_log_text_id = WebDriverWait(driver, timeout).until(EC.element_to_be_clickable((By.TAG_NAME, "body")))
        new_log_text_id.send_keys(log_text)
        driver.switch_to.default_content()

        wait_for_global_load(driver, timeout)
        save_btn_id = WebDriverWait(driver, timeout).until(
            EC.element_to_be_clickable((By.ID, "toolactions_SAVE-tbb_image")))
        save_btn_id.click()
        
        # 保存后等待页面加载完成,确保下一个工单处理时页面就绪
        wait_for_global_load(driver, timeout)
        print(f"Ticket {incident} processed successfully.")
    except Exception as err:
        print(f"An error occurred while processing ticket {incident}: {err}")
        # 出错后切回主文档,避免后续操作受iframe影响
        driver.switch_to.default_content()

def main():
    chrome_options = Options()
    chrome_options.add_argument("--disable-extensions")
    chrome_options.add_argument("--disable-gpu")
    chrome_options.add_argument("--window-size=1920,1080")
    chrome_options.add_argument("--no-sandbox")
    chrome_options.add_argument("--headless=new")
    chrome_options.add_argument("--disable-dev-shm-usage")

    driver = webdriver.Chrome(options=chrome_options)

    driver.get("https://www.company.website.com")
    id_element = WebDriverWait(driver, timeout).until(EC.element_to_be_clickable((By.ID, "j_username")))
    id_element.send_keys(email)
    pwd_element = WebDriverWait(driver, timeout).until(EC.element_to_be_clickable((By.ID, "j_password")))
    pwd_element.send_keys(pwd)
    pwd_element.send_keys(Keys.ENTER)
    
    wait_for_global_load(driver, timeout)
    incident_id = WebDriverWait(driver, timeout).until(EC.element_to_be_clickable((By.ID, "FavoriteApp_INCIDENT")))
    incident_id.click()

    # 批量处理工单
    for incident in incidents_list:
        ticket_processing(driver, incident)

if __name__ == '__main__':
    main()

额外优化建议

  • 将元素ID定义为常量(如QUICKSEARCH_ID = "quicksearch"),减少硬编码重复
  • 出错时添加截图逻辑:driver.save_screenshot(f"error_{incident}.png"),方便排查问题
  • 若页面加载较慢,可适当延长timeout时间,或添加重试机制

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 07:09:55