Run chrome browser in inconginto Mode in Selenium

给你一囗甜甜゛ 提交于 2019-12-17 17:59:51

问题


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.


回答1:


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.




回答2:


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.




回答3:


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:


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");


来源:https://stackoverflow.com/questions/19026295/run-chrome-browser-in-inconginto-mode-in-selenium

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!