How to Allow notification permission for desktop-notifications in Chrome Browser using python scripting



  • Hi i have been trying to automate a page using Selenium Python. And in one of the page it requires the user to grant permission for mic & cam. I have written the code for the same using the Chrom_Options() even then This dosn't help me in selecting the Allow permission automatically using the script.

    enter image description here

    Below is the code that I use and every time I need to manually click on the allow permission button

    from selenium.webdriver import Chrome
    from random import shuffle
    from time import sleep
    from moviepy.editor import VideoFileClip
    from selenium.common.exceptions import ElementClickInterceptedException
    import math
    from selenium.webdriver.chrome.options import Options
    
    #To Enable the notification (Allow Microphone and Camera)
    option = Options()
    
    option.add_argument("--enable-infobars")
    option.add_argument("start-maximized")
    option.add_argument("--enable-extensions")
    
    # Pass the argument 1 to allow and 2 to block
    option.add_experimental_option("prefs", {
        "profile.default_content_setting_values.notifications": 1
    })
    
    driver = Chrome(options=option, executable_path='/Users/downloads/chromedriver')
    driver.get('https://study.yupl.us/studies/5d5a64aa8b5bb92fb776753b?auth=false')
    
    def GetVideoDuration(video_element):
        val = VideoFileClip(video_element)
        return val.duration
    
    zip=[99524,90209]
    shuffle(zip)
    
    #click Start Study
    sleep(5)
    try:
        element = driver.find_element_by_xpath("//button[text()='START STUDY']")
        driver.execute_script("arguments[0].click();", element)
    except ElementClickInterceptedException:
        pass
    
    #Enter the ZIP code
    try:
        zipcode=driver.find_element_by_id('zipEntry_zipCode')
        zipcode.send_keys(zip[0])
    except ElementClickInterceptedException:
        pass
    
    #After entering Continue Button Click on Continue to Proceed
    try:
        continuebutton=driver.find_element_by_xpath('//span[text()="Continue"]')
        driver.execute_script("arguments[0].click();", continuebutton)
    except ElementClickInterceptedException:
        pass
    
    #Get The video duration and wait for the video to completly play
    videoduration=0
    try:
        sleep(10)
        questionvideo = driver.find_element_by_id('studyMediaVideo')
        video = driver.execute_script("return arguments[0].getAttribute('src')", questionvideo)
        videoduration = GetVideoDuration(video)
        # Play the video
        driver.execute_script("arguments[0].click();", questionvideo)
    except ElementClickInterceptedException:
        pass
    
    
    #wait till the video is completly played
    sleep(math.ceil(videoduration))
    
    #Click on Answer Question
    try:
        answer=driver.find_element_by_xpath('//button[text()="ANSWER"]')
        driver.execute_script("arguments[0].click();", answer)
    except ElementClickInterceptedException:
        pass
    


  • You have to add the following experimental options:

    "profile.default_content_setting_values.media_stream_mic" "profile.default_content_setting_values.media_stream_camera" "profile.default_content_setting_values.geolocation" "profile.default_content_setting_values.notifications"



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2