Running chrome browser in inconginto Mode in Selenium


We can run Chrome browser Incognito mode with Selenium webdriver. Incognito mode is a safe mode of opening a browser. This can be done with the help of the DesiredCapabilities and ChromeOptions class.

We shall create an object of the ChromeOptions class and apply addArguments method on it. Then pass −−incognito as a parameter to that method. We shall then create an object of the DesiredCapabilities class.

We shall apply setCapability method on the object of the DesiredCapabilities class and pass the ChromeOptions.CAPABILITY and the object of ChromeOptions class as parameters to that method.

Finally, this browser chrome profile shall be fed to the webdriver object.

Syntax

ChromeOptions o= new ChromeOptions();
o.addArguments("−−incognito");
DesiredCapabilities c = DesiredCapabilities.chrome();
c.setCapability(ChromeOptions.CAPABILITY, o);
WebDriver driver = new ChromeDriver(o);

Example

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
public class BrwIncognt{
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver",
      "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
      // configure options parameter to Chrome driver
      ChromeOptions o= new ChromeOptions();
      // add Incognito parameter
      o.addArguments("--incognito");
      // DesiredCapabilities object
      DesiredCapabilities c = DesiredCapabilities.chrome();
      //set capability to browser
      c.setCapability(ChromeOptions.CAPABILITY, o);
      WebDriver driver = new ChromeDriver(o);
      driver.get("https://www.tutorialspoint.com/index.htm ");
   }
}

Output

Updated on: 02-Feb-2021

11K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements