[英]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,此功能不可用。 在这种隐身模式下,您的位置历史记录或共享位置信息不会与您与之共享此信息的任何人更新。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.