22

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.

Alex Kulinkovich
  • 4,408
  • 15
  • 46
  • 50
kailash gaur
  • 1,407
  • 3
  • 15
  • 28

9 Answers9

31

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.

kailash gaur
  • 1,407
  • 3
  • 15
  • 28
  • Note latest version of selenium for python v3.141.0 uses `ChromeOptions.add_argument()` rather than addArguments(). – CodeMonkey Sep 17 '21 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.

blalasaadri
  • 5,990
  • 5
  • 38
  • 58
  • Thanks for your answer buddy,but is there any way we can do this in Selenium RC.I am using this version. – kailash gaur Sep 26 '13 at 11:36
  • According to [the api docs of DefaultSelenium](http://www.cloudtesting.com/selenium-docs/1.0.1/selenium-java-client-driver/com/thoughtworks/selenium/DefaultSelenium.html#DefaultSelenium(java.lang.String, 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) – blalasaadri Sep 26 '13 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);
evandrix
  • 6,041
  • 4
  • 27
  • 38
Maharshi Adiraju
  • 444
  • 1
  • 7
  • 13
3

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()
Ebran Khan
  • 21
  • 2
  • 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 '20 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
    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. – borchvm Sep 04 '20 at 05: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);
Jack Vicky
  • 81
  • 1
  • 5
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");
Pratik Patel
  • 115
  • 1
  • 2
  • 8
  • 4
    Hi and welcome to Stackoverflow! Rather than answering with just a block of code, can you [edit your answer](https://stackoverflow.com/posts/58799832/edit) 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 '19 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)
Dojo Arun
  • 21
  • 2