Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 404
John Smithv1 Avatar asked Dec 11 '14 14:12

John Smithv1


1 Answers

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:

  • Python/Selenium incognito/private mode
like image 196
alecxe Avatar answered Sep 21 '22 04:09

alecxe