Selenium Firefox插件:如何定位当前测试页并移除表单指定属性?
Hey there! Let's walk through how to remove that extra target attribute from the webform element using Selenium—both for the Firefox IDE plugin you're using and if you ever switch to code-based tests.
For Selenium IDE (Firefox Plugin)
Since you're building test cases with the IDE, follow these steps right after your open command:
- Add a Wait for Element Present command:
- Target:
id=webform - Value: (leave blank)
This makes sure the form element is fully loaded before we attempt to modify it, avoiding race conditions.
- Target:
- Add an Execute Script command:
- Target:
document.getElementById('webform').removeAttribute('target'); - Value: (leave blank)
This runs the exact same kind of JavaScript you'd use in the dev console to strip the unwantedtargetattribute.
- Target:
For Code-Based Tests (e.g., Python + Firefox Driver)
If you ever move to writing scripted tests, here's a reliable approach using explicit waits (way better than hardcoding sleep times):
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC # Initialize Firefox driver driver = webdriver.Firefox() # Open your test page driver.get("http://aaa.com") # Wait up to 10 seconds for the webform element to be present wait = WebDriverWait(driver, 10) webform_element = wait.until(EC.presence_of_element_located((By.ID, "webform"))) # Execute JS to remove the target attribute driver.execute_script("arguments[0].removeAttribute('target');", webform_element) # Add your subsequent test actions here... # Clean up the driver driver.quit()
Quick Note
The core idea here is leveraging Selenium's ability to run custom JavaScript—just like you do in the browser's dev tools. Passing the pre-located element to execute_script (via arguments[0]) is more reliable than querying the DOM again in JS, since it ensures you're modifying the exact element Selenium has already found.
内容的提问来源于stack exchange,提问作者zaozaoer

