简体   繁体   中英

Options().add_experimental_option does not work

I've been working on a script to automate a task in Google Chrome. Unfortunately, I haven't had any luck in giving Chrome access to my microphone and denying access to notifications. I have tried using Options().add_experimental_option() but without any luck. Am I doing something wrong or is there another way? I have tried using fake UI and fake media stream to remove the popup, but that hasn't worked either. Options().add_argument("--use-fake-ui-for-media-stream"); Options().add_argument("--use-fake-device-for-media-stream")

import time
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver import ActionChains
from selenium.webdriver.chrome.options import Options

Options().binary_location = "C:\Program Files\Google\Chrome\Application\chrome.exe"
Options().add_argument("start-maximized");
Options().add_argument("--disable-infobars")
# 1 to allow, 2 to block
Options().add_experimental_option("prefs", { \
   "profile.default_content_setting_values.media_stream_mic": 1,
   "profile.default_content_setting_values.media_stream_camera": 2,
   "profile.default_content_setting_values.geolocation": 2,
   "profile.default_content_setting_values.notifications": 2
})
driver=webdriver.Chrome(".\chromedriver.exe", options=Options())

You need to add all the attributes , arguments and experimental option within a single instance of Options and pass as an argument while initiating the ChromeDriver / combo as follows:

import time
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver import ActionChains
from selenium.webdriver.chrome.options import Options

options = Options()
options.binary_location = "C:\Program Files\Google\Chrome\Application\chrome.exe"
options.add_argument("start-maximized")
options.add_argument("--disable-infobars")
# 1 to allow, 2 to block
options.add_experimental_option("prefs", { \
   "profile.default_content_setting_values.media_stream_mic": 1,
   "profile.default_content_setting_values.media_stream_camera": 2,
   "profile.default_content_setting_values.geolocation": 2,
   "profile.default_content_setting_values.notifications": 2
})
driver=webdriver.Chrome(".\chromedriver.exe", options=options)

References

You can find a couple of relevant detailed discussions in:

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