Workday脚本填充字段后保存报错,字段值未被系统正确识别
问题描述
我写了个脚本自动填充Workday的表单字段,输入框里已经显示内容了,但点“保存并继续”时弹出错误:
公司:该字段为必填项,必须输入值。
学校或大学:该字段为必填项,必须输入值。
相关脚本代码:
let jobNames = ["XXX"] let startMonths = ["XXX"] let endMonths = ["XXX"] let startYears = ["XXX"] let endYears = ["XXX"] let companyNames = ["XXX"] let jobDescriptions = ["XXX"] let currentWork = ["False"] for(let i = 0; i < companyNames.length; i++) { let x = document.querySelector(`div[data-automation-id="workExperience-${i + 1}"]`) let button; if(!x) { if(i == 0) { button = document.querySelector('button[data-automation-id="Add"]') } else{ button = document.querySelector('button[data-automation-id="Add Another"]') } if (button) { button.click() await new Promise(resolve => setTimeout(resolve, 1000)); } } let jobInput = document.querySelectorAll(`input[data-automation-id="jobTitle"]`)[i] let companyNameInput = document.querySelectorAll(`input[data-automation-id="company"]`)[i] let jobDescriptionInput = document.querySelectorAll(`textarea[data-automation-id="description"]`)[i] let monthInput = document.querySelectorAll(`input[data-automation-id="dateSectionMonth-input"]`)[2 * i] let monthInput2 = document.querySelectorAll(`input[data-automation-id="dateSectionMonth-input"]`)[(2 * i) + 1] let yearInput = document.querySelectorAll(`input[data-automation-id="dateSectionYear-input"]`)[2 * i] let yearInput2 = document.querySelectorAll(`input[data-automation-id="dateSectionYear-input"]`)[(2 * i) + 1] if (jobInput) { await typeText(jobInput, jobNames[i]) jobInput.dispatchEvent(new Event('input', { bubbles: true })); jobInput.focus() } if (companyNameInput) { await typeText(companyNameInput, companyNames[i]) //companyNameInput.value = companyNames[i]; companyNameInput.dispatchEvent(new Event('change', { bubbles: true })); } if (jobDescriptionInput) { jobDescriptionInput.value = jobDescriptions[i]; jobDescriptionInput.dispatchEvent(new Event('input', { bubbles: true })); } if (monthInput) { monthInput.value = startMonths[i]; monthInput.dispatchEvent(new Event('input', { bubbles: true })); monthInput.focus(); // Focus on the input element monthInput.click(); // Simulate click event monthInput.click(); // Simulate click event } if (monthInput2) { monthInput2.value = endMonths[i]; monthInput2.dispatchEvent(new Event('input', { bubbles: true })); monthInput2.focus(); // Focus on the input element monthInput2.click(); // Simulate click event monthInput2.click(); // Simulate click event } if (yearInput) { yearInput.value = startYears[i]; yearInput.dispatchEvent(new Event('input', { bubbles: true })); yearInput.focus(); // Focus on the input element yearInput.click(); // Simulate click event yearInput.click(); // Simulate click event } if (yearInput2) { yearInput2.value = endYears[i]; yearInput2.dispatchEvent(new Event('input', { bubbles: true })); yearInput2.focus(); // Focus on the input element yearInput2.click(); // Simulate click event yearInput2.click(); // Simulate click event } } }
解决方法
Workday这类企业级应用大多基于React、Vue等框架,直接修改value或只触发单一事件,无法同步框架内部的表单状态,导致后端校验认为字段未填写。试试下面几个调整:
1. 触发完整的事件链
框架通常会监听多种事件更新状态,把单一事件替换成input+change+blur的组合触发:
// 公司输入框示例 if (companyNameInput) { await typeText(companyNameInput, companyNames[i]); companyNameInput.dispatchEvent(new Event('input', { bubbles: true, cancelable: true })); companyNameInput.dispatchEvent(new Event('change', { bubbles: true, cancelable: true })); companyNameInput.blur(); // 模拟失去焦点触发内部校验 companyNameInput.focus(); }
2. 用逐字符输入替代直接赋值
直接给value赋值会绕过框架的状态监听,确保typeText是逐字符输入的函数,每个字符输入都触发input事件:
async function typeText(input, text) { input.value = ''; for (const char of text) { input.value += char; input.dispatchEvent(new Event('input', { bubbles: true, cancelable: true })); await new Promise(resolve => setTimeout(resolve, 50)); // 模拟真实输入间隔 } }
3. 优化元素等待逻辑
固定1秒延迟可能赶不上Workday的动态加载速度,改用等待元素存在的工具函数:
async function waitForSelector(selector, timeout = 5000) { const start = Date.now(); while (Date.now() - start < timeout) { const element = document.querySelector(selector); if (element) return element; await new Promise(resolve => setTimeout(resolve, 100)); } throw new Error(`元素 ${selector} 超时未出现`); } // 使用示例:获取第i个公司输入框 const companyNameInput = await waitForSelector(`input[data-automation-id="company"]:nth-of-type(${i+1})`);
4. 针对Workday特殊字段的处理
公司、学校这类带联想搜索的输入框,输入后可以按下Enter键模拟选择结果,确保系统识别为已填写:
companyNameInput.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter', bubbles: true }));
内容的提问来源于stack exchange,提问作者CodingChef
相关产品推荐
相关产品推荐

