Search code examples
pythonselenium-webdriverfirefox-profile

Adding extension through selenium firefox profile success. But when I open these Firefox manually, the extension still not installed


I use selenium python. My code work success, the extension was added. But when I close the code, open the Firefox Profile which added extension by manually then the extension isn't installed. My code

from selenium import webdriver from selenium.webdriver.common.by import By import time

try:

path = "My_profile_PATH"
fp = webdriver.FirefoxProfile(path)     
driver = webdriver.Firefox(firefox_profile=fp)

# path to your downloaded Firefox addon extension XPI file

extension_path = "MY_extension_PATH"

# using webdriver's install_addon API to install the downloaded Firefox extension

driver.install_addon(extension_path, temporary=True)

# Opening the Firefox support page to verify that addon is installed

driver.get("about:support")

# xpath to the section on the support page that lists installed extension

addons = driver.find_element(By.XPATH,'//*[contains(text(),"Add-ons") and not(contains(text(),"with"))]')
# scrolling to the section on the support page that lists installed extension

driver.execute_script("arguments[0].scrollIntoView();", addons)

# introducing program halt time to view things, ideally remove this when performing test automation in the cloud using LambdaTest

print("Success. Yayy!!")

time.sleep(20)

except Exception as E:

print(E)

finally:

# exiting the fired Mozilla Firefox selenium webdriver instance

driver.quit()

# End Of Script

Solution

  • When you ask Selenium to use a profile, it copies it to a temporary location and use this copy. So whatever you do doesn't affect the original profile.

    If you want your script to affect the original profile, don't ask Selenium to use a profile. Instead, tell Firefox directly which profile to use:

    from selenium import webdriver
    from selenium.webdriver.firefox.options import Options
    
    options = Options()
    options.add_argument('-profile')
    options.add_argument('/path/to/your/profile')
    
    driver = webdriver.Firefox(options=options)