Python/Selenium 隐身/私密模式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27630190/
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-19 02:02:40  来源:igfitidea点击:

Python/Selenium incognito/private mode

pythonseleniumbrowserselenium-webdriverincognito-mode

提问by BubblewrapBeast

I can not seem to find any documentation on how to make Selenium open the browser in incognito mode.

我似乎找不到任何关于如何让 Selenium 在隐身模式下打开浏览器的文档。

Do I have to setup a custom profile in the browser or?

我是否必须在浏览器中设置自定义配置文件?

采纳答案by alecxe

First of all, since seleniumby default starts up a browser with a clean, brand-new profile, you are actually already browsing privately. Referring to:

首先,由于selenium默认情况下浏览器会使用干净的全新配置文件启动,因此您实际上已经在进行私密浏览。参考:



But you can strictly enforce/turn on incognito/private mode anyway.

但是无论如何您都可以严格执行/打开隐身/私密模式。

For chrome pass --incognitocommand-line argument:

对于 chrome pass--incognito命令行参数

--incognitoCauses the browser to launch directly in incognito mode.

--incognito使浏览器直接以隐身模式启动。

from selenium import webdriver

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

driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get('https://google.com')

FYI, here is what it would open up:

仅供参考,这是它会打开的内容:

happy holidays!

节日快乐!

For firefox, set browser.privatebrowsing.autostartto True:

对于火狐,设置browser.privatebrowsing.autostartTrue

from selenium import webdriver

firefox_profile = webdriver.FirefoxProfile()
firefox_profile.set_preference("browser.privatebrowsing.autostart", True)

driver = webdriver.Firefox(firefox_profile=firefox_profile)

FYI, this corresponds to the following checkbox in settings:

仅供参考,这对应于设置中的以下复选框:

enter image description here

在此处输入图片说明

回答by Adarsha

PowerShell

电源外壳

try{
    # Import the Selenium DLLs
    Add-Type -Path "$Seleniumlib\Selenium.WebDriverBackedSelenium.dll"
    Add-Type -Path "$Seleniumlib\WebDriver.dll"
    Add-Type -Path "$Seleniumlib\WebDriver.Support.dll"
}
catch [Exception]{
    Write-Host ("Error: {0}" -f $_.Exception.Message)
    exit 1
}

$options = New-Object OpenQA.Selenium.Chrome.ChromeOptions
$options.AddArgument("--incognito")
$driver = New-Object OpenQA.Selenium.Chrome.ChromeDriver($options)

回答by Gavriel Cohen

There is a really simple way to make a window open in incognito mode:

有一种非常简单的方法可以在隐身模式下打开窗口:

from selenium.webdriver.chrome.options import Options

chrome_options = Options()
# incognito window
chrome_options.add_argument("--incognito")

You can also use this library for maximizing the window and more, see the documentation: https://seleniumhq.github.io/selenium/docs/api/rb/Selenium/WebDriver/Chrome/Options.html

您还可以使用此库来最大化窗口等,请参阅文档:https: //seleniumhq.github.io/selenium/docs/api/rb/Selenium/WebDriver/Chrome/Options.html

回答by Kireeti Annamaraj

I have initiated both Chrome and Firefox in incognito/Private mode using ChromeOptions and FirefoxOptions successfully using the code snippets in Java as below:

我已经使用 ChromeOptions 和 FirefoxOptions 在隐身/隐私模式下成功启动了 Chrome 和 Firefox,使用 Java 中的代码片段如下:

    //For Firefox
    FirefoxOptions options = new FirefoxOptions();
    options.addArguments("-private");
    DesiredCapabilities caps = new DesiredCapabilities();
    caps.setCapability("moz:firefoxOptions",options);

    //For Chrome
    ChromeOptions options = new ChromeOptions();
    options.addArguments("-incognito");
    caps.setCapability(ChromeOptions.CAPABILITY, options);

    WebDriver driver = new RemoteWebDriver(new URL(URL), caps);

回答by A_Asaker

For firefox : (Python) ==>

对于火狐:(Python)==>

from selenium import webdriver    
firefox_options = webdriver.FirefoxOptions()
firefox_options.add_argument("--private")
browser = webdriver.Firefox(firefox_options=firefox_options)

回答by Swaraj S

Note: chrome_options is now deprecated. We can use 'options' instead of chrome_options

注意: chrome_options 现在已弃用。我们可以使用 'options' 而不是 chrome_options

from selenium import webdriver

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

driver = webdriver.Chrome(options=options)
driver.get('https://google.com')

回答by chawlajc

In Chrome Browser You Can Do This Using Python As Follows

在 Chrome 浏览器中,您可以使用 Python 执行此操作,如下所示

As you can see when you uses chrome, you have the option of incognito mode in the options menu part of the chrome browser. So when you are using selenium, you can alter the things of options using

正如您在使用 chrome 时所看到的,您可以在 chrome 浏览器的选项菜单部分选择隐身模式。因此,当您使用 selenium 时,您可以使用更改选项的内容

chrome_options = webdriver.ChromeOptions()

So, the code is:

所以,代码是:

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

driver = webdriver.Chrome(executable_path="<path of chrome_driver.exe file>",options=chrome_options)

So the only thing you have to do is to give "webdriver.Chrome" this given value to its another parameter i.e. "options".

因此,您唯一要做的就是将这个给定的值赋予“webdriver.Chrome”另一个参数,即“options”。

回答by Mariobarbosa777

For python with opera

对于带有歌剧的蟒蛇

from selenium import webdriver

options =  webdriver.opera.webdriver.Options()
options.add_argument("private")
driver = webdriver.Opera(executable_path="operadriver",options=options)