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

Python-在私有模式下使用Selenium启动firefox,python,firefox,selenium,selenium-webdriver,Python,Firefox,Selenium,Selenium Webdriver,我有以下脚本: #!/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 = webd

我有以下脚本:

#!/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的点:

Selenium相当于打开私人浏览

以及以下各项的定义:

私人浏览允许您浏览Internet而不保存任何内容 有关您访问过的站点和页面的信息

因为每次你通过SeleniumWebDriver启动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)
另见:


@Louis我以前只看过这些问题。检查答案,我同意你的看法,另一个答案更好。我认为应该关闭浏览器。Firefox处理私人浏览的方式存在技术差异。当Firefox的变体在私人浏览下强制执行某些行为而不是“保存任何信息”时,模拟这些行为非常重要。值得注意的是,扩展的处理方式非常不同。@ndm13好的,谢谢您的反馈。更新了答案。此代码的复制/粘贴不适用于我(最新的Linux/Mint发行版)。一切正常,除了它不是以匿名模式启动。有什么想法吗?