DEV Community

Akram Siddiqui
Akram Siddiqui

Posted on

How to handle Permission Pop-ups using Selenium WebDriver | Selenium |Python|

Hello Dev Community,

I'm currently working on Selenium WebDriver automation with Python, focusing on handling permission pop-ups in headless mode on a Linux machine. Specifically, I want to handle the location permission alert and allow it to make my tests run smoothly.

In non-headless mode, the following code works perfectly:

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.chrome.options import Options as ChromeOptions
from time import sleep

# Chrome options setup
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--incognito')

# Configure content settings to disable notifications and geolocation
content_settings = {'notifications': 0, 'geolocation': 0}
profile = {'managed_default_content_settings': content_settings}
prefs = {'profile': profile}
chrome_options.add_experimental_option('prefs', prefs)

# Add arguments to disable notifications, geolocation, and media stream
chrome_options.add_argument('--disable-notifications')
chrome_options.add_argument('--disable-geolocation')
chrome_options.add_argument('--use-fake-ui-for-media-stream')

path = r"C:\Program Files (x86)\chromedriver-win64\chromedriver.exe"

# WebDriver setup using WebDriver Manager
service = ChromeService(path)
driver = webdriver.Chrome(service=service, options=chrome_options)
driver.maximize_window()

# Navigate to the specified URLs
driver.get('https://www.cleartrip.com/')
driver.get('https://whatmylocation.com/')

sleep(15)
Enter fullscreen mode Exit fullscreen mode

However, when I switch to headless mode on a Linux machine, the location permission alert is not being handled as expected. Here's the headless mode code:

def create_headless_chrome_driver():
    chrome_options = ChromeOptions()
    chrome_options.add_argument('--start-maximized')
    chrome_options.add_argument('--headless')

    # Allow geolocation
    chrome_options.binary_location = CHROME_BINARY_PATH
    chrome_options.add_argument('window-size=1920x1080')
    chrome_options.add_argument("--no-sandbox")
    chrome_options.add_argument("--disable-dev-shm-usage")

    # Handling geolocation
    chrome_options.add_argument('--incognito')

    # Configure content settings to disable notifications and geolocation
    content_settings = {'notifications': 1, 'geolocation': 1}
    profile = {'managed_default_content_settings': content_settings}
    prefs = {'profile': profile}
    chrome_options.add_experimental_option('prefs', prefs)

    # Add arguments to disable notifications, geolocation, and media stream
    chrome_options.add_argument('--disable-notifications')
    chrome_options.add_argument('--disable-geolocation')
    chrome_options.add_argument('--use-fake-ui-for-media-stream')

    service = ChromeService(executable_path=CHROMEDRIVER_PATH)
    driver = webdriver.Chrome(service=service, options=chrome_options)

    return driver
Enter fullscreen mode Exit fullscreen mode

**

Top comments (1)

Collapse
 
proteusiq profile image
Prayson Wilfred Daniel • Edited

Could you use python highlight code blocks like

from selenium import webdriver

# this is a comment
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--incognito')
Enter fullscreen mode Exit fullscreen mode