使用HAR Export XPI+Selenium+FFv46生成HAR文件失败排查
Let's walk through the likely gaps you're missing—I've wrestled with old Firefox + HAR extension setups before, so these are the common pain points:
Confirm the HAR Export XPI is properly installed and active in your profile
First, take Selenium out of the equation: manually launch Firefox 46 using your configured profile. Head toabout:addonsand check that the HAR Export extension is listed and enabled. If it's not showing up, you might have used an XPI incompatible with FF46, or your profile didn't pick up the extension correctly. Install the XPI manually viaabout:addons> Install Add-on From File, then save the profile to reuse with Selenium.Disable extension signature checks
Even FF46 can block unsigned extensions in some builds. Open your profile'sprefs.jsfile and add this line:user_pref("xpinstall.signatures.required", false);This ensures the HAR Export XPI (which likely isn't signed for modern Firefox) loads without being blocked.
Fix the timing of your JavaScript injection
TheHARobject showing up asundefinedmeans your script is running before the extension finishes initializing. Instead of injecting right after page load, add a poll to wait for theHARobject to exist. For example:function waitForHAR(callback) { if (window.HAR) { callback(); } else { setTimeout(() => waitForHAR(callback), 500); } } waitForHAR(() => { // Run your HAR export logic here, e.g. HAR.triggerExport() });This gives the extension time to load its API into the window context.
Double-check your Selenium profile setup
Make sure Selenium is actually using your pre-configured profile. It's easy to accidentally create a temporary profile instead of pointing to the one with the HAR extension. For example, in Python:from selenium.webdriver.firefox.options import Options from selenium.webdriver.firefox.firefox_profile import FirefoxProfile profile = FirefoxProfile('/path/to/your/configured/profile') options = Options() options.profile = profile # Initialize your driver with these optionsCheck for extension load errors
Enable Firefox logging in your Selenium setup to catch if the HAR extension is throwing errors on startup. Add these preferences:options.set_preference("browser.dom.window.dump.enabled", True) options.log.level = "trace"Scan the logs for lines mentioning the HAR extension failing to load or register its API.
If the HAR object appears when you test the profile manually but not in Selenium, the issue is almost certainly related to Selenium's profile loading or your script injection timing.
内容的提问来源于stack exchange,提问作者Kallol Ghose

