Splash Lua脚本实现下拉框选择功能问题求助
Fixing Lua/Splash Dropdown Selection for
right_eye_156 Hey, let's get that dropdown selection working for you! Your current script only fetches the page title, so we need to add logic to target the right_eye_156 select element and set it to the 8.4 option. Here's a revised script with explanations:
function main(splash, args) -- Navigate to the target URL and wait for initial page load assert(splash:go(splash.args.url)) assert(splash:wait(1)) -- Adjust wait time if your page loads slower -- JavaScript to interact with the dropdown local dropdown_script = [[ // Grab the dropdown element by its ID (most reliable way) const selectEl = document.getElementById('right_eye_156'); // Set the value to match the 8.4 option's value attribute selectEl.value = '8.4'; // Trigger a 'change' event—critical for frameworks like Vue/React/jQuery to detect the update const changeEvent = new Event('change'); selectEl.dispatchEvent(changeEvent); ]] -- Execute the dropdown manipulation script splash:evaljs(dropdown_script) -- Wait a moment for the page to process the dropdown change (optional but recommended) splash:wait(0.5) -- Return whatever data you need (title, HTML, etc.) return { page_title = splash:evaljs("document.title"), updated_html = splash:html() -- Useful for verifying the change worked } end
Key Notes:
- ID Selection: Using
getElementByIdis more reliable than class selectors here since IDs are unique to a single element. - Value Matching: Double-check the HTML of your dropdown! If the 8.4 option has a different
valueattribute (e.g.,<option value="84">8.4</option>), replace'8.4'with the actual value from the option tag. - Triggering
changeEvent: Many modern web apps rely on this event to update UI or send requests—skipping this step might make it look like the dropdown didn't change. - Wait Times: Adjust the
splash:wait()values if your page takes longer to load or process the dropdown update.
If You Only Know the Option Text (Not Value):
If you don't have the option's value and need to select by the visible text "8.4", use this version of the dropdown_script instead:
local dropdown_script = [[ const selectEl = document.getElementById('right_eye_156'); // Loop through options to find the one with text "8.4" for (let i = 0; i < selectEl.options.length; i++) { if (selectEl.options[i].text.trim() === '8.4') { selectEl.selectedIndex = i; break; } } const changeEvent = new Event('change'); selectEl.dispatchEvent(changeEvent); ]]
Give this a try—this should resolve your dropdown manipulation issue!
内容的提问来源于stack exchange,提问作者Alex D
相关产品推荐
相关产品推荐

