Run chrome browser in inconginto Mode in Selenium
Asked Answered
S

10

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.

Substandard answered 26/9, 2013 at 10:56 Comment(0)
S
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.

Substandard answered 4/10, 2013 at 8:0 Comment(1)
Note latest version of selenium for python v3.141.0 uses ChromeOptions.add_argument() rather than addArguments().Farmer
M
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.

Merchandise answered 26/9, 2013 at 11:16 Comment(2)
Thanks for your answer buddy,but is there any way we can do this in Selenium RC.I am using this version.Substandard
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)Merchandise
R
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);
Raeraeann answered 29/10, 2016 at 15:20 Comment(0)
S
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);
Sabir answered 25/4, 2020 at 8:16 Comment(0)
C
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()
Chrysler answered 23/5, 2020 at 16:8 Comment(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.Hogfish
B
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");  
Biles answered 4/9, 2020 at 5:6 Comment(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.Acidophil
S
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);
Side answered 21/12, 2022 at 15:43 Comment(0)
W
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");
Whitton answered 11/11, 2019 at 10:43 Comment(1)
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.Caliph
O
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)
Openwork answered 5/10, 2022 at 8:48 Comment(0)
M
-1

use --incognito

Forces Incognito mode even if user data directory is specified using the --user-data-dir switch.

https://peter.sh/experiments/chromium-command-line-switches/#incognito

Mosa answered 4/4 at 6:35 Comment(1)
Provide the complete code solutionSoteriology

© 2022 - 2024 — McMap. All rights reserved.