简体   繁体   English

如何在 Python Selenium 的 Chrome 隐身模式下允许位置和通知?

[英]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 .我想使用Selenium在隐身模式下允许 Chrome 上的位置和通知。

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).如果不包含option.add_argument("--incognito") (浏览器以正常模式打开),则此代码有效(启用通知和位置)。 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. Asil Açku 根据您的问题和您的评论,我对您要完成的工作没有明确的处理。

The code below opens a Chrome browser in incognito mode using Selenium.下面的代码使用Selenium在隐身模式下打开 Chrome 浏览器 The website being accessed is Google Maps with an exact geolocation.正在访问的网站是具有准确地理位置的 Google 地图。 Once the page loads Google automatically obtains my current location.页面加载后,Google 会自动获取我的当前位置。 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.您在 Google 地图中提到了有关“共享您的实时位置”的内容。 Since you want to open Chrome in incognito mode, this capability isn't available.由于您想在隐身模式下打开 Chrome,此功能不可用。 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 Chrome 中的 Google 地图位置共享在此处输入图片说明

Google Maps Location Sharing in incognito mode隐身模式下的 Google 地图位置共享在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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