Python-在私有模式下使用Selenium启动firefox

这个问题已经在这里有了答案:            >            Python/Selenium incognito/private mode                                    7个
我有以下脚本:

#!/usr/bin/python3
from selenium import webdriver
import time

def getProfile():
    profile = webdriver.FirefoxProfile()
    profile.set_preference("browser.privatebrowsing.autostart", True)
    return profile

def main():
    browser = webdriver.Firefox(firefox_profile=getProfile())

    #browser shall call the URL
    browser.get("http://www.google.com")
    time.sleep(5)
    browser.quit()

if __name__ == "__main__":
    main()

如何管理Firefox以私有模式启动?

最佳答案
参考@Laas在How might I simulate a private browsing experience in Watir? (Selenium)处的观点:

Selenium is equivalent to turning on Private Browsing.

以及“Private Browsing”的定义:

Private Browsing allows you to browse the Internet without saving any
information about which sites and pages you’ve visited.

而且,由于每次通过Selenium Webdriver启动Firefox都会创建一个全新的匿名配置文件,因此您实际上是在私下浏览.

如果仍然要在Firefox中强制使用私有模式,请将browser.privatebrowsing.autostart配置选项设置为true:

from selenium import webdriver

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

driver = webdriver.Firefox(firefox_profile=firefox_profile)

另请参阅:

> Python/Selenium incognito/private mode

转载注明原文:Python-在私有模式下使用Selenium启动firefox - 代码日志