Python/Selenium Incognito/Private Mode

Python/Selenium incognito/private mode

First of all, since selenium by default starts up a browser with a clean, brand-new profile, you are actually already browsing privately. Referring to:

  • Python - Start firefox with Selenium in private mode
  • How might I simulate a private browsing experience in Watir? (Selenium)

But you can strictly enforce/turn on incognito/private mode anyway.

For chrome pass --incognito command-line argument:

--incognito Causes the browser to launch directly in incognito mode.

from selenium import webdriver

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--incognito")

driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get('https://google.com')

FYI, here is what it would open up:

happy holidays!

For firefox, set browser.privatebrowsing.autostart 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)

FYI, this corresponds to the following checkbox in settings:

Sample Image

How to add extension in incognito mode when launched from selenium chrome webdriver with python

Rather you loading the required cookies/extension as part of your chrome options, other option is using the chrome profile. Check my answer in this post

To more on the profiles and how they work refer here

Here is the logic to turn on the extension in the incognito mode.

Python:

# driver.get("chrome://extensions/?id=extion_name_goes_here"); # <=== general snippet see below example
# driver.get("chrome://extensions/?id=jfpmbokkdeapjommajdfmmheiiakdlgo")

# select allow in incognito mode checkbox
driver.execute_script("return document.querySelector('extensions-manager').shadowRoot.querySelector('#viewManager > extensions-detail-view.active').shadowRoot.querySelector('div#container.page-container > div.page-content > div#options-section extensions-toggle-row#allow-incognito').shadowRoot.querySelector('label#label input').click()");

Refer to my answer in this post for more information on the js used above.

How to allow Location and Notifications in Chrome incognito mode in Python Selenium?

Asil Açku based on your question and your comments, I don't have a firm handled on what you are trying to accomplish.

The code below opens a Chrome browser in incognito mode using Selenium. The website being accessed is Google Maps with an exact geolocation. Once the page loads Google automatically obtains my current location. It does this with no browser notifications.

If this isn't what you need please provide more details.

from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium import webdriver

capabilities = DesiredCapabilities().CHROME

chrome_options = Options()
chrome_options.add_argument("--incognito")
chrome_options.add_argument("--disable-infobars")
chrome_options.add_argument("start-maximized")
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--disable-popup-blocking")

prefs = {
'profile.default_content_setting_values':
{
'notifications': 1,
'geolocation': 1
},

'profile.managed_default_content_settings':
{
'geolocation': 1
},
}

chrome_options.add_experimental_option('prefs', prefs)
capabilities.update(chrome_options.to_capabilities())

driver = webdriver.Chrome('/usr/local/bin/chromedriver', options=chrome_options)

url='https://www.google.com/maps/@48.1152117,-1.6634771,12.79z/data=!5m1!1e1'
driver.get(url)

# time used only for testing
time.sleep(10)

You mentioned something about "sharing your live location" in Google Maps. Since you want to open Chrome in incognito mode, this capability isn't available. In this cloaked mode your location history or shared location information will not be updated with anyone that you are are sharing this information with.

Google Maps Location Sharing in Chrome
Sample Image

Google Maps Location Sharing in incognito mode
Sample Image

How to open Microsoft Edge (Chromium) in private mode with Selenium Python 3.x?

I agree with the suggestion given by the @RichEdwards

I suggest try to check the points below may help you to narrow down and fix the issue.

  1. Make sure you are using the correct version of the web driver. check your browser version and download the appropriate driver from here. It can be better if you can make a test with the latest stable version of the MS Edge browser.

  2. Make sure that you had installed the MS Edge Selenium tools using command below.

pip install msedge-selenium-tools selenium==3.141

Sample code:

from msedge.selenium_tools import Edge, EdgeOptions

options = EdgeOptions()
options.use_chromium = True
options.add_argument("-inprivate")
options.binary_location = r"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"
driver = Edge(executable_path = r"D:\<driver path>\msedgedriver.exe", options = options) # Modify the path here...

# Navigate to URL
driver.get("https://example.com")

# Access web elements

driver.find_element_by_id('fname').send_keys("ABC")

driver.find_element_by_id('lname').send_keys("XYZ")

driver.quit

Output:

Sample Image



Related Topics



Leave a reply



Submit