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

Joh*_*hv1 9 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 = 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()
Run Code Online (Sandbox Code Playgroud)

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

ale*_*cxe 14

参考@ Laas关于如何在Watir中模拟私人浏览体验的观点(硒):

Selenium相当于打开私人浏览.

"私人浏览"的定义:

私密浏览允许您浏览Internet而不保存有关您访问过的网站和页面的任何信息.

而且每次你​​通过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)
Run Code Online (Sandbox Code Playgroud)

另见,见: