ultrafunkamsterdam / undetected-chromedriver

Custom Selenium Chromedriver | Zero-Config | Passes ALL bot mitigation systems (like Distil / Imperva/ Datadadome / CloudFlare IUAM)
https://github.com/UltrafunkAmsterdam/undetected-chromedriver
GNU General Public License v3.0
9.06k stars 1.09k forks source link

Enable Camera and Mic preferences on browser? #452

Open DGaffney opened 2 years ago

DGaffney commented 2 years ago

I'm looking to enable camera and mic by default in the preferences - and not getting too far. Tried to implement: https://stackoverflow.com/questions/48007699/how-to-allow-or-deny-notification-geo-location-microphone-camera-pop-up - and getting the following results after running this demo script:

from pyvirtualdisplay import Display
size=(1600, 900)
display = Display(size=size)
display.start()
import undetected_chromedriver as uc
chrome_options = uc.ChromeOptions()  
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument("--user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36")
chrome_options.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 
})
chrome = uc.Chrome(version_main=96, options=chrome_options, headless=False)
chrome.get("https://bot.sannysoft.com")
chrome.save_screenshot("screen.png")

Result:

>>> from pyvirtualdisplay import Display
>>> size=(1600, 900)
>>> display = Display(size=size)
>>> display.start()
<pyvirtualdisplay.display.Display object at 0x7f591c435d30>
>>> import undetected_chromedriver as uc
>>> chrome_options = uc.ChromeOptions()  
>>> chrome_options.add_argument('--no-sandbox')
>>> chrome_options.add_argument("--user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36")
>>> chrome_options.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 
... })
>>> chrome = uc.Chrome(version_main=96, options=chrome_options, headless=False)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.9/dist-packages/undetected_chromedriver/__init__.py", line 369, in __init__
    super(Chrome, self).__init__(
  File "/usr/local/lib/python3.9/dist-packages/selenium/webdriver/chrome/webdriver.py", line 70, in __init__
    super(WebDriver, self).__init__(DesiredCapabilities.CHROME['browserName'], "goog",
  File "/usr/local/lib/python3.9/dist-packages/selenium/webdriver/chromium/webdriver.py", line 93, in __init__
    RemoteWebDriver.__init__(
  File "/usr/local/lib/python3.9/dist-packages/selenium/webdriver/remote/webdriver.py", line 268, in __init__
    self.start_session(capabilities, browser_profile)
  File "/usr/local/lib/python3.9/dist-packages/undetected_chromedriver/__init__.py", line 552, in start_session
    super(selenium.webdriver.chrome.webdriver.WebDriver, self).start_session(
  File "/usr/local/lib/python3.9/dist-packages/selenium/webdriver/remote/webdriver.py", line 359, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/usr/local/lib/python3.9/dist-packages/selenium/webdriver/remote/webdriver.py", line 424, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python3.9/dist-packages/selenium/webdriver/remote/errorhandler.py", line 247, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: cannot parse capability: goog:chromeOptions
from invalid argument: unrecognized chrome option: prefs
Stacktrace:
#0 0x55e3b36c1ee3 <unknown>
#1 0x55e3b318f608 <unknown>
#2 0x55e3b31a8acf <unknown>
#3 0x55e3b31ad075 <unknown>
#4 0x55e3b31a56d2 <unknown>
#5 0x55e3b31e7c8b <unknown>
#6 0x55e3b31e76c6 <unknown>
#7 0x55e3b31e8d4d <unknown>
#8 0x55e3b31e3163 <unknown>
#9 0x55e3b31b8bfc <unknown>
#10 0x55e3b31b9c05 <unknown>
#11 0x55e3b36f3baa <unknown>
#12 0x55e3b3709651 <unknown>
#13 0x55e3b36f4b05 <unknown>
#14 0x55e3b370aa68 <unknown>
#15 0x55e3b36e905f <unknown>
#16 0x55e3b3725818 <unknown>
#17 0x55e3b3725998 <unknown>
#18 0x55e3b3740eed <unknown>
#19 0x7f8af1539ea7 <unknown>

Anyone have thoughts on how to fix this?

gokulprathin8 commented 2 years ago

Hi @DGaffney ,

instead of using prefs, trying this


driver.execute_cdp_cmd(
            "Browser.grantPermissions",
            {
                "origin": YOUR_URL,   # e.g https://www.google.com
                "permissions": ["geolocation", "audioCapture", "displayCapture", "videoCapture",
                                "videoCapturePanTiltZoom"]
            },
        )

this will allow permissions for geolocation, audio, video, and screen capture. but you will have to set the URL upfront.

DGaffney commented 2 years ago

That seems to work fine with https sites but not http sites - is that part of the spec for these types of permissions?