'How do I use extensions in incognito

I wanna use python selemium incognito with extensions but none worked so far Anyone got a fix?

I tried:

options.add_extension('plugin.crx')
        #options.add_argument("--incognito")
        #options.add_argument("user-data-dir=C:\\Users\\Maxva\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 9")
        options.add_experimental_option('excludeSwitches', ['enable-logging'])
        driver = webdriver.Chrome(options=options)
        
        #driver.get('chrome://extensions/?id=lncaoejhfdpcafpkkcddpjnhnodcajfg')
        
        #driver.execute_script("return document.querySelector('extensions-manager').shadowRoot.querySelector('#viewManager > extensions-detail-view.active').shadowRoot.querySelector('div#container.page-container > div.page-content > div#options-section extensions-toggle-row#allow-incognito').shadowRoot.querySelector('label#label input').click()")


Solution 1:[1]

My favourite trick is to use the keyboard:

options.add_argument("--incognito")
options.add_extension('Google-Translate.crx')

driver.get("chrome://extensions")
time.sleep(1)
action = ActionChains(driver)

# Open the extension:
for _ in range(2):
    action.send_keys(Keys.TAB).perform()
action.send_keys(Keys.RETURN).perform()

# Flip the switch that enables it in incognito
for _ in range(4):
    action.send_keys(Keys.TAB).perform()
action.send_keys(Keys.RETURN).perform()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1