23

I want to run chrome in incongito mode through selenium. I googled enough for it and found how to run chrome directly in incongito mode with the help of this link:

  1. Right click on the shortcut of Google Chrome and select "Properties".
  2. On "Shortcut" tab on the "Target" field add an –incognito to the end of program path. So in the "Target" field you should have "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" –incognito

but I didn't get how to run this in selenium.

0

9 Answers 9

32

One other way to launch chrome in incognito mode is to add argument "-incognito" like following:

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

This solution works for me.

1
  • Note latest version of selenium for python v3.141.0 uses ChromeOptions.add_argument() rather than addArguments().
    – CodeMonkey
    Sep 17, 2021 at 15:52
10

According to the ChromeDriver wiki you can pass parameters to the executable like this:

DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.switches", Arrays.asList("--incognito"));
WebDriver driver = new ChromeDriver(capabilities);

So passing the paremeter --incognito should do the trick.

2
  • Thanks for your answer buddy,but is there any way we can do this in Selenium RC.I am using this version. Sep 26, 2013 at 11:36
  • According to [the api docs of DefaultSelenium](cloudtesting.com/selenium-docs/1.0.1/…, int, java.lang.String, java.lang.String)) it is possible to pass a full startup command to the constructor; so rather than calling new DefaultSelenium("localhost", 8080, "*googlechrome", "http://www.stackoverflow.com") you could try new DefaultSelenium("localhost", 8080, "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe -incognito", "http://www.stackoverflow.com") (or similar) Sep 26, 2013 at 12:38
6

The code below will open the browser in incognito mode using selinium. Assuming selenium is setup in your eclipse:

public WebDriver chromedriver;
ChromeOptions options = new ChromeOptions();
options.addArguments("--incognito");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver chromedriver=new ChromeDriver(capabilities);
4

When you use Selenium.WebDriver3.14.0 with ChromeDriver 81 bellow code should work.

ChromeOptions options = new ChromeOptions();
options.AddArgument("--incognito");

Driver = new ChromeDriver(options);
2
from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument("--incognito")

caps = options.to_capabilities()

browser = webdriver.Chrome(desired_capabilities=caps)
browser.get('https://amazon.in')

browser.quit()
1
  • While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. May 23, 2020 at 22:26
1
    System.setProperty("webdriver.chrome.driver", "path for chromedriver.exe");
    ChromeOptions options = new ChromeOptions();
    options.addArguments("incognito");
    DesiredCapabilities cap = DesiredCapabilities.chrome();
    cap.setCapability(ChromeOptions.CAPABILITY, options);
    driver = new ChromeDriver(cap);
    driver.get("webpage URL");  
1
  • 1
    While this code may provide a solution to the question, it's better to add context as to why/how it works. This can help future users learn, and apply that knowledge to their own code. You are also likely to have positive feedback from users in the form of upvotes, when the code is explained.
    – Borja
    Sep 4, 2020 at 5:59
1

IN selenium 4.6 DesiredCapabilities was depreciated so we can use ChromeOptions, try the below 3 step code.

ChromeOptions options = new ChromeOptions();
options.addArguments("--incognito");
this.driver = new ChromeDriver(options);
0
System.setProperty("webdriver.chrome.driver", "path for chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("incognito");
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new ChromeDriver(cap);
driver.get("https://google.com");
1
  • 4
    Hi and welcome to Stackoverflow! Rather than answering with just a block of code, can you edit your answer and give a (short) explanation as to what the problem was you solved, and how you solved it? This will help people understand your answer better and learn from it.
    – Plutian
    Nov 11, 2019 at 10:47
0
from selenium import webdriver
baseUrl = ""
options = webdriver.ChromeOptions()
options.add_argument("--incognito")
capability = options.to_capabilities()
driver = webdriver.Chrome(desired_capabilities=capability)
driver.get(baseUrl)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.