首页 > 解决方案 > 如何始终允许在 Gecko 驱动程序或 Python Selenium 上的网站中定位

问题描述

嗨,我想始终允许位置或特定网站,但我无法解决我的问题。

现在我打开页面信息并手动设置,但我想以编程方式进行。

fp = webdriver.FirefoxProfile('Path'
                          '-1602622171076')
fp.set_preference("browser.privatebrowsing.autostart", True)
driver = webdriver.Firefox(firefox_profile=fp, executable_path='Path')
driver.get(url)

标签: pythonseleniumgeolocationlocation

解决方案


1.对于铬:

from selenium.webdriver.chrome.options import Options

# to accept all the notifications

opt = Options()
opt.add_argument("--disable-infobars")
opt.add_argument("start-maximized")
opt.add_argument("--disable-extensions")
opt.add_experimental_option("prefs", { \
    "profile.default_content_setting_values.media_stream_mic": 1,
    "profile.default_content_setting_values.media_stream_camera": 1,
    "profile.default_content_setting_values.geolocation": 1,
    "profile.default_content_setting_values.notifications": 1
})

它是接受所有通知的代码

1 表示同意

0 表示禁用

如果你想禁用任何只是用 0 替换 1

  1. 对于 Mozilla 火狐:
from selenium.webdriver.firefox.options import Options

options = Options()

options.set_preference("dom.webnotifications.enabled", True)

driver = webdriver.Firefox(firefox_options=options)

推荐阅读