2

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 ?

asp
  • 777
  • 3
  • 14
  • 33
  • Does opening the profile with preinstalled extension in incognito mode, ok with you? – supputuri Jun 01 '19 at 14:11
  • Hello @supputuri how are you ? actually i read some posts where they were talking about profile, but could not understand its concept. whati is the profile with preinstalled extension ? can you guide pls. It seems incognito modes does not allow adding/enabling extensions when selenium is being used ? – asp Jun 01 '19 at 14:16
  • 1
    I am working on solution for your issue. Will post the answer shortly. Will also make sure to address your question in comment. – supputuri Jun 01 '19 at 14:20
  • Thank you. Here my observation, without incognito mode, it will have all extension by default. when i open chrome in manually in incognito(not through python/selenium) still i see all extension in place. but when we use webdriver with incogito then it wont show any extension, even adding by add extension method does not work..not sure if that is limitation of selenium .hmm – asp Jun 01 '19 at 14:23

2 Answers2

5

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.

supputuri
  • 13,644
  • 2
  • 21
  • 39
  • check this and let me know, if you have any questions. – supputuri Jun 01 '19 at 14:51
  • Thanks for explanation,will try above method, but I have even tried with driver.get("chrome://extensions/?id=extion_name_goes_here") method and with ID too...the problem here is once you launch chrome in incognito via webdriver, then i see no extension there. I tried get(chrome://extension) it showed nothing there – asp Jun 01 '19 at 15:09
  • Update: created new profile on chrome, tried to add extension, still it wont add extension. I cant see this extension in list.. – asp Jun 01 '19 at 15:18
  • Did you try adding the extension manually? If not `add it manually to the new profile`. Then remove add extension in your script. – supputuri Jun 01 '19 at 15:27
  • Hello sir i have used (as per your post) options.add_argument("user-data-dir=d:\\abprofile") so it created a profile at that location, how can i launch chrome from that profile to add extension manually? – asp Jun 01 '19 at 15:33
  • Hello sir, I guess i made it now. ran program with that new prfile,added extension without closing selenium broweser session from chrome store. and went to option and enabled allow in incognio option and ran program multiple times i can see extension enabled(In this case I did not close webdriver expllicitly). However only thing i need to check if close or quit browser, whether it will retain extension or not – asp Jun 01 '19 at 15:56
  • Wow sir..so creating new profile at different location did the trick and we need to add extension from that websession. bingo.. one more doubt, i am reading certain url from text file like below `with open("d:/urllist.txt") as in_file: for url in in_file: # get website driver.get(url.strip())` but it reading only first url in the list..any mistakes in this code.. – asp Jun 01 '19 at 16:02
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/194295/discussion-between-asp-and-supputuri). – asp Jun 01 '19 at 16:36
0

Here is my code snippet that automatically opens list of extensions and turns off extension while in incognito mode.

However, pls use this code only if you already considered and tried using another clean profile as was suggested by users above.


extension_name = "chrome_proxy_extension"

## select Chrome proxy extension
ss = """
var t1 = document.querySelector("extensions-manager").shadowRoot;
var t2 = t1.querySelector("extensions-item-list").shadowRoot;
var lst_ext = t2.querySelectorAll("extensions-item");

var idx_proxy = -1
for (let idx = 0; idx < lst_ext.length; idx++) {
    var el = lst_ext[idx]
    if (el.shadowRoot.innerHTML.indexOf("$EXTENSION_NAME$")>0){
        idx_proxy = idx
    };    
    };
    
var btn = lst_ext[idx_proxy].shadowRoot.querySelector("cr-button")
btn.click()

"""  
ss = ss.replace("$EXTENSION_NAME$", extension_name)

## select toggle with "allow in incognito mode" and press it
ss2 = """
var t1 = document.querySelector("extensions-manager").shadowRoot;
var t2 = t1.querySelector("extensions-detail-view").shadowRoot;
var opt_hide = t2.querySelector("extensions-toggle-row#allow-incognito");
var toggle = opt_hide.shadowRoot.querySelector("cr-toggle");

if (toggle.checked){
    console.log("Incognito mode is already on")
} else {

toggle.click()
}
"""

time.sleep(2)
browser.get("chrome://extensions")
time.sleep(2)
browser.execute_script(ss)
time.sleep(2)
browser.execute_script(ss2)
time.sleep(2)