You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Python Selenium自动化时,如何同时正常使用Firefox浏览器?

Got it, let's fix this so you can use your regular Firefox window while the automation runs without any conflicts! The root issue here is that by default, Selenium's Firefox instance might share (or interfere with) your default Firefox profile. The solution is to create a separate, dedicated Firefox profile for your automation scripts—this way, the two Firefox instances run completely independently.

Step 1: Create a new Firefox profile for automation

  1. Open your regular Firefox browser, type about:profiles into the address bar and hit Enter.
  2. Click the "Create a New Profile" button, follow the wizard to name it (e.g., "SeleniumAutomation") and choose a save location (make sure to remember this path!).
  3. Once created, you'll see the profile's "Root Directory" path—copy this, you'll need it in your code.

Step 2: Modify your Python code to use this dedicated profile

You'll need to initialize the Firefox driver with your new profile instead of using the default one. Here's the updated version of your code with this change, plus some reliability improvements:

from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# Replace with the actual root directory path of your new automation profile
PROFILE_PATH = "/path/to/your/SeleniumAutomation/profile/root/directory"

# Configure Firefox to use the custom profile
options = Options()
options.profile = PROFILE_PATH

# Launch the automation browser with the dedicated profile
browser = webdriver.Firefox(options=options)

with open("links.txt", "r") as my_file:
    for url in my_file:
        url = url.strip()  # Clean up any extra newlines/spaces in the URL
        browser.get(url)
        
        # Wait for elements to be clickable (more reliable than fixed time.sleep)
        WebDriverWait(browser, 10).until(
            EC.element_to_be_clickable((By.CSS_SELECTOR, 'button.popover'))
        ).click()
        
        WebDriverWait(browser, 10).until(
            EC.element_to_be_clickable((By.CSS_SELECTOR, 'div.faux_field'))
        ).click()
        
        WebDriverWait(browser, 10).until(
            EC.element_to_be_clickable((By.CSS_SELECTOR, 'button.apply'))
        ).click()
        
        WebDriverWait(browser, 10).until(
            EC.element_to_be_clickable((By.CSS_SELECTOR, 'button.click-to-esign'))
        ).click()

# Clean up: close the automation browser when finished
browser.quit()

Quick Tips:

  • Double-check that PROFILE_PATH matches the exact path you copied from about:profiles—wrong paths will break the setup.
  • Swapping time.sleep for WebDriverWait makes your script more resilient: it waits only until the element is ready to interact with, instead of wasting time or failing if the page loads slower than expected.
  • Now when you run the script, it’ll launch a Firefox instance tied to the dedicated automation profile, and you can use your regular Firefox window totally normally—no more cross-instance conflicts!

内容的提问来源于stack exchange,提问作者Joe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:18:19