Python - Start firefox with Selenium in private mode [duplicate]
Asked Answered
L

1

16

I have the following script:

#!/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()

How can I manage Firefox to start in private mode?

Linebacker answered 11/12, 2014 at 14:23 Comment(2)
@Louis I only looked at the questions before. Checking the answers, I have to agree with you that the other answer is better.Mulvaney
I think the #27630690 should be closed instead.Meraz
R
24

Referring to the @Laas's point at How might I simulate a private browsing experience in Watir? (Selenium):

Selenium is equivalent to turning on Private Browsing.

And the definition of "Private Browsing":

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

And since every time you start firefox through selenium webdriver it creates a brand new anonymous profile, you are actually browsing privately.


If you still want to force the private mode in Firefox, set the browser.privatebrowsing.autostart configuration option to true:

from selenium import webdriver

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

driver = webdriver.Firefox(firefox_profile=firefox_profile)

Also, see:

Reimers answered 11/12, 2014 at 14:30 Comment(4)
There are technical differences with how Firefox handles private browsing. Simulating these is important when variations of Firefox enforce certain behaviors under private browsing other than not "saving any information". Notably, extensions are handled very differently.Phosphorescence
@Phosphorescence okay, thanks for the feedback. Updated the answer.Reimers
Copy/paste of this code doesnt work for me (latest Linux/Mint distro). Everything works except that it's not started in incognito mode. Any idea?Phenyl
TypeError: __init__() got an unexpected keyword argument 'firefox_profile'Armistice

© 2022 - 2024 — McMap. All rights reserved.