简体   繁体   中英

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

I want to allow location and notifications on Chrome in incognito mode using Selenium .

Here is my code:

import selenium
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
option = Options()

option.add_argument("--incognito")
option.add_argument("--disable-infobars")
option.add_argument("start-maximized")
option.add_argument("--disable-extensions")

option.add_experimental_option("prefs", {
    "profile.default_content_setting_values.notifications":1,
    "profile.default_content_setting_values.geolocation": 1,
})

driver = webdriver.Chrome(options=option, executable_path="path/to/executable")

This code works (Notifications and Location are enabled) if option.add_argument("--incognito") isn't included (the browser is opened in normal mode). Why is this happening and how can I enable these along with incognito mode?

Note: You can check if the location is enabled by open a website that requests your location.

Browser's view in incognito: 在此处输入图片说明

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 在此处输入图片说明

Google Maps Location Sharing in incognito mode在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM