IE中.Click事件因onchange异常,网站隐藏标签搜索自动化求助
Hey there, let's tackle these two automation issues one by one—here are actionable solutions tailored to your problems:
IE’s event handling is notoriously quirky, especially when onchange and click events interfere with each other. Here are a few reliable workarounds:
- Explicitly trigger onchange before clicking: If the
onchangeevent is tied to an input value change, fire it manually before triggering the click. For example, using Selenium:input_element = driver.find_element(By.ID, "your-input-id") input_element.send_keys("new-value") # Manually trigger the onchange event driver.execute_script("arguments[0].onchange();", input_element) # Now execute the click click_element = driver.find_element(By.ID, "your-click-target-id") click_element.click() - Simulate native JavaScript events: IE sometimes ignores synthetic events from automation tools. Try dispatching proper
changeandclickevents directly via JS:// Trigger change event on the input const input = document.getElementById('your-input-id'); const changeEvent = document.createEvent('Event'); changeEvent.initEvent('change', true, true); input.dispatchEvent(changeEvent); // Trigger click on the target element const btn = document.getElementById('your-click-target-id'); const clickEvent = document.createEvent('Event'); clickEvent.initEvent('click', true, true); btn.dispatchEvent(clickEvent); - Check for event blocking: Inspect the
onchangehandler code—if it callsevent.preventDefault()orevent.stopPropagation(), that could be blocking the click. If you have access to modify the code, remove those lines; if not, bypass the handler via JS execution.
Let’s break down the steps to interact with this site reliably:
Step 1: Activate the Hidden "Legislação" Tab
The tab is activated via the inline onclick function on the link with id="lk_leg". Automation tools might not trigger this correctly with a simple click, so execute the JS directly:
# Using Selenium as an example # First, execute the tab activation script driver.execute_script("move_layer('legislacao', parent.hiddenFrame.layerX);") # Wait for the tab content to load (critical to avoid race conditions) from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.NAME, "leg_campo1")))
Alternatively, if the script relies on the link’s context, click the element first before executing the JS:
leg_tab = driver.find_element(By.ID, "lk_leg") leg_tab.click() # Then add the wait as above
Step 2: Fill the "Todos os campos" Field
Once the tab is active, locate the field by its name attribute and populate it:
search_field = driver.find_element(By.NAME, "leg_campo1") search_field.clear() # Clear any pre-filled value search_field.send_keys("your-search-query") # Trigger the search (locate the submit button, adjust the selector as needed) search_btn = driver.find_element(By.XPATH, "//input[@type='submit' or @value='Pesquisar']") search_btn.click()
Additional Tips for This Site
- Frame Handling: The site uses a
hiddenFrame—if elements aren’t found, check if they’re inside an iframe and switch to it:driver.switch_to.frame("hiddenFrame") # Perform your actions here driver.switch_to.default_content() # Switch back when done - Avoid Hard Sleeps: Use explicit waits (like
WebDriverWait) instead oftime.sleep()—this makes your automation more stable for older, slow-loading sites.
内容的提问来源于stack exchange,提问作者danieltakeshi

