3

I am using Selenium 3.4 to launch Edge using the Microsoft WebDriver which is now maintained by Microsoft.

Is there any way I can launch the Browser in InPrivate mode using Selenium?

I have searched for answers but couldn't find any.

The closest I got was How to start Edge browser in Incognito mode using selenium remote webdriver?

The solution mentioned there doesn't work. It just shows the same tab as would be shown in InPrivate, but the window isn't a private one. As such, the information is stored and the session is not private.

TylerH
  • 19,065
  • 49
  • 65
  • 86
Mayank Nehru
  • 31
  • 1
  • 4
  • In this condition there is 1 option to ensure you start clean.On your machine or VM,select the option to clear all browsing history when browser closes .https://www.onmsft.com/news/how-to-automatically-delete-your-microsoft-edge-browsing-history .This will ensure you start fresh. – user7258708 May 30 '18 at 12:39

3 Answers3

2

Add capabilities... Try the code below.

DesiredCapabilities capabilities = DesiredCapabilities.edge();
capabilities.setCapability("ms:inPrivate", true);
return new EdgeDriver(capabilities);
tryingToLearn
  • 7,566
  • 8
  • 55
  • 84
Aswin T A
  • 15
  • 3
1

I made the capabilities work in Python like this:

from selenium.webdriver import Edge
from selenium.webdriver import DesiredCapabilities

capabilities = DesiredCapabilities.EDGE
capabilities['ms:inPrivate'] = True
driver = Edge(capabilities=capabilities) 
0

Use the below code, employing java.awt.Robot to simulate the key combination CTRL+SHIFT+P to open a new browser window in InPrivate mode:

    System.setProperty("webdriver.edge.driver","D:\\Workspace\\StackOverlow\\src\\lib\\MicrosoftWebDriver.exe"); //put actual location
    WebDriver driver = new EdgeDriver();

    driver.navigate().to("https://www.google.com");
    driver.manage().window().maximize();

     Robot robot;
    try {
        // This is the actual code that opens the InPrivate window
        robot = new Robot();
        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_SHIFT);
        robot.keyPress(KeyEvent.VK_P);
        robot.keyRelease(KeyEvent.VK_CONTROL);
        robot.keyRelease(KeyEvent.VK_SHIFT);
        robot.keyRelease(KeyEvent.VK_P);
        Thread.sleep(3000);
    } catch (AWTException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    String parentWindowHandler = driver.getWindowHandle(); 
    String subWindowHandler = null;

    Set<String> handles = driver.getWindowHandles();
    Iterator<String> iterator = handles.iterator();
    while (iterator.hasNext()){
        subWindowHandler = iterator.next();
        driver.switchTo().window(subWindowHandler);

        System.out.println(subWindowHandler);
    }

    driver.get("https://stackoverflow.com/");
    //driver.switchTo().window(parentWindowHandler);   // Uncomment this line if you want to use normal browser back
}

Note that we are using robot class and so if the system locks it may not work.

mapto
  • 541
  • 8
  • 22
Shubham Jain
  • 13,158
  • 9
  • 60
  • 102
  • Which part exactly has anything to do with InPrivate browsing? – mapto Sep 27 '18 at 12:46
  • Robot part ... But may as UI change it code need to update – Shubham Jain Sep 27 '18 at 13:06
  • My bad. Not being aware of how Robot works I didn't get the way you do it. I've tried it now myself and it works. However, I'll need to add to your answer in order to be able to undo my downvote and upvote it. Give me a sec to do it. – mapto Sep 27 '18 at 13:40