Java 在 Selenium 中以 inconginto 模式运行 Chrome 浏览器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19026295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 13:30:26  来源:igfitidea点击:

Run chrome browser in inconginto Mode in Selenium

javagoogle-chromeseleniumselenium-rc

提问by kailash gaur

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

我想incongito通过 selenium在模式下运行 chrome 。我在谷歌上搜索了足够多的内容,并找到了如何在此链接incongito的帮助下直接在模式下运行 chrome :

  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
  1. 右键单击 Google Chrome 的快捷方式并选择“属性”。
  2. 在“目标”字段的“快捷方式”选项卡上,将 –incognito 添加到程序路径的末尾。所以在“目标”字段中你应该有"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" –incognito

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

但我不知道如何在 selenium 中运行它。

回答by blalasaadri

According to the ChromeDriver wikiyou can pass parameters to the executable like this:

根据ChromeDriver wiki,您可以像这样将参数传递给可执行文件:

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

So passing the paremeter --incognitoshould do the trick.

因此,传递参数--incognito应该可以解决问题。

回答by kailash gaur

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

在隐身模式下启动 chrome 的另一种方法是添加参数“-incognito”,如下所示:

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

This solution works for me.

这个解决方案对我有用。

回答by Maharshi Adiraju

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

下面的代码将使用 selinium 在隐身模式下打开浏览器。假设在你的 eclipse 中设置了 selenium:

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

回答by Pratik Patel

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

回答by Rajitha Kithuldeniya

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

当您使用 Selenium.WebDriver3.14.0 和 ChromeDriver 81 时,波纹管代码应该可以工作。

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

Driver = new ChromeDriver(options);

回答by Ebran Khan

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()