pythonseleniummicrosoft-edge

Selenium Python Edge disable `open office files in browser` setting


Python: 3.9.9
Selenium: 4.1.5
Edge: 101.0.1210.39 (X64) driver link

I am trying to automate downloading excel file from a website, but due to Edge's default setting of open office files in browser set to True, on pressing download button with selenium it redirects to Edge file viewer instead of downloading it.
Since I want to automate the process I don't want to manually go to settings and disable it every time.

Any work arounds will be appreciated too...
Thank you!


Solution

  • This is what worked for me:

    from pathlib import Path
    from selenium import webdriver
    
    if Path('..\msedgedriver.exe').exists():
        driver = webdriver.Edge('..\msedgedriver.exe')
    
        # Settings
        driver.get('edge://settings/downloads')
        toggle = driver.execute_script('''
                return document.querySelector(' input[aria-label="Open Office files in 
        the browser"]');
        ''')
        toggle.click()
    
    # continue...
    

    similarly you can change any setting as per required.