簡體   English   中英

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

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

我想使用Selenium在隱身模式下允許 Chrome 上的位置和通知。

這是我的代碼:

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")

如果不包含option.add_argument("--incognito") (瀏覽器以正常模式打開),則此代碼有效(啟用通知和位置)。 為什么會發生這種情況,以及如何在隱身模式下啟用這些功能?

注意:您可以通過打開一個請求您的位置的網站來檢查該位置是否已啟用。

瀏覽器在隱身模式下的視圖: 在此處輸入圖片說明

Asil Açku 根據您的問題和您的評論,我對您要完成的工作沒有明確的處理。

下面的代碼使用Selenium在隱身模式下打開 Chrome 瀏覽器 正在訪問的網站是具有准確地理位置的 Google 地圖。 頁面加載后,Google 會自動獲取我的當前位置。 它在沒有瀏覽器通知的情況下執行此操作。

如果這不是您需要的,請提供更多詳細信息。

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)

您在 Google 地圖中提到了有關“共享您的實時位置”的內容。 由於您想在隱身模式下打開 Chrome,此功能不可用。 在這種隱身模式下,您的位置歷史記錄或共享位置信息不會與您與之共享此信息的任何人更新。

Chrome 中的 Google 地圖位置共享在此處輸入圖片說明

隱身模式下的 Google 地圖位置共享在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM