简体   繁体   中英

How to add extension in incognito mode when launched from selenium chrome webdriver with python

We are doing some testing using selenium python webdriver where we need to open one url in incognito mode and enable one extension already installed and then do some actions.

My findings:

  • loading of extension in incognito mode not working
  • extension getting loaded when icgnito mode turned off

Verified so many post on stack overflow, nothing worked. tried below code"

path = os.path.dirname(r"C:\Users\ab\AppData\Local\Google\Chrome\User Data\Default\Extensions\jfpmbokkdeapjommajdfmmheiiakdlgo\0.1.7_0\manifest.json")
options = webdriver.ChromeOptions()
options.add_argument('--incognito')
options.add_argument("--load-extension={path}")
driver = webdriver.Chrome(chrome_options=options, executable_path='C:\chromedriver_win32\chromedriver.exe')
driver.maximize_window()
driver.get(xxxxxxxx)

which throwing error cannot load manifest.json either missing or not readable. However i have made sure the path is correct.

any suggestion please how to load extension while opening chrome driver in incognito mode ?

Rather you loading the required cookies/extension as part of your chrome options, other option is using the chrome profile. Check my answer in this post

To more on the profiles and how they work refer here

Here is the logic to turn on the extension in the incognito mode.

Python:

# driver.get("chrome://extensions/?id=extion_name_goes_here"); # <=== general snippet see below example
# driver.get("chrome://extensions/?id=jfpmbokkdeapjommajdfmmheiiakdlgo") 

# select allow in incognito mode checkbox
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()");

Refer to my answer in this post for more information on the js used above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM