Skip to content

Instantly share code, notes, and snippets.

@vchrombie
Created November 11, 2024 18:35
Show Gist options
  • Save vchrombie/ba171e058770ed83c95a3d3849931c56 to your computer and use it in GitHub Desktop.
Save vchrombie/ba171e058770ed83c95a3d3849931c56 to your computer and use it in GitHub Desktop.
(async function() {
// Mapping of dropdown field IDs to desired answers
const dropdownFieldAnswers = {
'input-83': 'Yes', // Do you certify you meet all minimum qualifications...
'input-85': 'Opt-In to receive text messages from Walmart', // Would you like to receive mobile text message updates...
'input-89': 'Yes', // Are you legally able to work...
'input-91': '18 years of age and Over', // Please select your age category
'input-93': 'Have never been an employee of Walmart Inc or any of its subsidiaries', // Walmart Associate Status/Affiliation
'input-99': 'Yes', // Are you able to provide work authorization within 3 days...
'input-101': 'No', // Will you now or in the future require sponsorship...
'input-105': 'No', // Do you have Active Duty or Guard/Reserve experience...
'input-113': 'No', // Are you the Spouse/Partner of someone in the Uniformed Services...
'input-115': 'No' // Do you have a direct family member who currently works...
};
// Mapping of input field selectors to desired answers
const inputFieldAnswers = {
'#input-87': '7329868521' // Phone number field
};
/**
* Helper function to wait for a specific element to appear in the DOM
* @param {string} selector - The CSS selector of the element
* @param {number} timeout - Maximum time to wait in milliseconds
* @returns {Promise<Element>}
*/
function waitForElement(selector, timeout = 5000) {
return new Promise((resolve, reject) => {
const interval = 100;
let elapsed = 0;
const timer = setInterval(() => {
const element = document.querySelector(selector);
if (element) {
clearInterval(timer);
resolve(element);
}
elapsed += interval;
if (elapsed >= timeout) {
clearInterval(timer);
reject(new Error(`Element ${selector} not found within ${timeout}ms`));
}
}, interval);
});
}
/**
* Function to set the value of a dropdown field
* @param {string} fieldId - The ID of the button associated with the dropdown
* @param {string} value - The value to select
*/
async function setDropdown(fieldId, value) {
try {
const button = await waitForElement(`#${fieldId}`, 3000);
if (!button) {
console.warn(`Button with ID "${fieldId}" not found.`);
return;
}
// Click the dropdown to open it
button.click();
console.log(`Clicked dropdown for field "${fieldId}".`);
// Wait for the dropdown options to appear
await waitForElement('ul[role="listbox"]', 2000);
const options = Array.from(document.querySelectorAll('ul[role="listbox"] li'));
// Normalize value for comparison
const normalizedValue = value.trim().toLowerCase();
const desiredOption = options.find(option => option.textContent.trim().toLowerCase() === normalizedValue);
if (desiredOption) {
desiredOption.click();
console.log(`Set field "${fieldId}" to "${value}".`);
} else {
console.warn(`Option "${value}" not found for field "${fieldId}". Available options: ${options.map(o => o.textContent.trim()).join(', ')}`);
}
// Optional: Wait a short period before proceeding to the next field
await new Promise(res => setTimeout(res, 500));
} catch (error) {
console.error(`Error setting field "${fieldId}":`, error);
}
}
/**
* Function to set the value of a text input field
* @param {string} selector - The CSS selector for the input field
* @param {string} value - The value to input
*/
async function setTextInput(selector, value) {
try {
const input = await waitForElement(selector, 3000);
if (!input) {
console.warn(`Input field "${selector}" not found.`);
return;
}
input.value = value;
input.dispatchEvent(new Event('input', { bubbles: true }));
input.dispatchEvent(new Event('change', { bubbles: true }));
console.log(`Set input "${selector}" to "${value}".`);
} catch (error) {
console.error(`Error setting input "${selector}":`, error);
}
}
// Sequentially fill dropdown fields
for (const [fieldId, answer] of Object.entries(dropdownFieldAnswers)) {
await setDropdown(fieldId, answer);
}
// Fill input fields
for (const [selector, value] of Object.entries(inputFieldAnswers)) {
await setTextInput(selector, value);
}
console.log('Form auto-fill script executed.');
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment