14

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?

Nakilon
  • 34,866
  • 14
  • 107
  • 142
John Smithv1
  • 673
  • 5
  • 14
  • 33
  • 1
    @Louis I only looked at the questions before. Checking the answers, I have to agree with you that the other answer is better. – Uyghur Lives Matter Dec 24 '14 at 18:49
  • I think the http://stackoverflow.com/questions/27630190/python-selenium-incognito-private-mode should be closed instead. – Nakilon Jan 10 '16 at 00:07

1 Answers1

22

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:

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • 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. – ndm13 Dec 18 '15 at 01:42
  • 1
    @ndm13 okay, thanks for the feedback. Updated the answer. – alecxe Dec 18 '15 at 14:48
  • 1
    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? – Olivier Pons Apr 18 '18 at 06:49
  • `TypeError: __init__() got an unexpected keyword argument 'firefox_profile'` – axolotl Jun 12 '23 at 20:33