Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open incognito/private window with Selenium WD for different browser types?

I want to test my test cases in private window or incognito window.

How to do the same in various browsers:

  • firefox (prefered)
  • chrome (prefered)
  • IE
  • safari
  • opera

How to achieve it?

like image 736
atul chaurasia Avatar asked Oct 19 '15 21:10

atul chaurasia


People also ask

How do I open incognito in different browsers?

The easiest way to open an Incognito window is with the keyboard shortcut combination Ctrl-Shift-N (Windows) or Command-Shift-N (macOS). Another way is to click on the menu on the upper right - it's the three vertical dots - and select New Incognito Window from the list.

Does Selenium open incognito mode browser?

We can open a browser window in incognito/private mode with Selenium webdriver in Python using the ChromeOptions class.

How do I make my incognito window private?

In Incognito, none of your browsing history, cookies and site data, or information entered in forms are saved on your device. This means your activity doesn't show up in your Chrome browser history, so people who also use your device won't see your activity.

How do I open multiple tabs in incognito mode?

To open an incognito tab in Chrome Browser, one has to tap on the three dots at the corner of the screen and then select the New incognito tab option. A new window with an incognito tab will open in front of you, further to open multiple incognito tabs tap on the plus icon present at the side of the tab.


2 Answers

  • Chrome:

    DesiredCapabilities capabilities = DesiredCapabilities.chrome();
    ChromeOptions options = new ChromeOptions();
    options.addArguments("incognito");
    capabilities.setCapability(ChromeOptions.CAPABILITY, options);
    
  • FireFox:

    FirefoxProfile firefoxProfile = new FirefoxProfile();    
    firefoxProfile.setPreference("browser.privatebrowsing.autostart", true);
    
  • Internet Explorer:

    DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
    capabilities.setCapability(InternetExplorerDriver.FORCE_CREATE_PROCESS, true); 
    capabilities.setCapability(InternetExplorerDriver.IE_SWITCHES, "-private");
    
  • Opera:

    DesiredCapabilities capabilities = DesiredCapabilities.operaBlink();
    OperaOptions options = new OperaOptions();
    options.addArguments("private");
    capabilities.setCapability(OperaOptions.CAPABILITY, options);
    
like image 111
agabrys Avatar answered Sep 25 '22 20:09

agabrys


In chrome you can try using -incognito command line switch in options, not sure if there will be a problem with automation extension but worth a try.

ChromeOptions options = new ChromeOptions();
options.addArguments("incognito");

For FireFox, a special flag in the profile can be used for the purpose

FirefoxProfile firefoxProfile = new FirefoxProfile();    
firefoxProfile.setPreference("browser.private.browsing.autostart",true);

For IE

setCapability(InternetExplorerDriver.IE_SWITCHES, "-private");
like image 36
rac2030 Avatar answered Sep 25 '22 20:09

rac2030