3

I am facing a problem automating the protection settings in IE using Selenium with python.

I found a solution to automate the settings in java but it is not working when i changed it to python .

I tried the following::

from selenium import webdriver

caps=webdriver.DesiredCapabilites.INTERNETEXPLORER
caps['INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS']=True
driver=webdriver.Ie(caps)

This gave an error with respect to the argument given .

and when I used driver = webdriver.Ie() It says protection mode settings must be same for all zones.

Can anyone help me automate this thing using selenium in python.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
user2587419
  • 69
  • 1
  • 2
  • 6

4 Answers4

5

According to documentation, in python-selenum, you should use setting called ignoreProtectedModeSettings:

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

caps = DesiredCapabilities.INTERNETEXPLORER
caps['ignoreProtectedModeSettings'] = True

driver = webdriver.Ie(capabilities=caps)
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • 1
    +1, but you should **seriously** fix the underlying issue.... http://jimevansmusic.blogspot.co.uk/2012/08/youre-doing-it-wrong-protected-mode-and.html ....by that I mean, **don't do this anyway!** – Arran Jul 16 '13 at 13:59
  • Thank You for the answer. This solution is working when i use it along with IEDriverServer. However when i try to use this piece of code with selenium 2.26 or so where ie can be opened without IEDriverServer, I am unable to do achieve this. Can you tell me how i can replicate this for earlier selenium versions where IEDriverServer is optional – user2587419 Jul 18 '13 at 13:04
  • Changing the settings manually is an easy option and i have relied on it so far. But i work on an automation framwork which invokes ie on virtual machines. The problem that i am encountering is that although i change the settings for protected mode manually , when i invoke ie from remote machines ,it is trying to load ie with default settigs and hence it is again saying that protection zone levels must be set to single value. That is the reason i am trying to automate even the protected mode settings. – user2587419 Jul 18 '13 at 13:12
  • Can someone please mention the documentation link where 'ignoreProtectedModeSettings' is mentioned. I couldn't find it anywhere on Internet. – Underoos Aug 27 '18 at 10:02
  • Vivek's answer below helped me. https://stackoverflow.com/a/55745242/8055011 It's an updated version of this answer – Harshit Jindal May 01 '21 at 15:26
2

Desired capabilities doesn't work in some instances. Here is a method to change protection settings from the registry using winreg.

from winreg import *

    def Enable_Protected_Mode():
        # SECURITY ZONES ARE AS FOLLOWS:
        # 0 is the Local Machine zone
        # 1 is the Intranet zone
        # 2 is the Trusted Sites zone
        # 3 is the Internet zone
        # 4 is the Restricted Sites zone
        # CHANGING THE SUBKEY VALUE "2500" TO DWORD 0 ENABLES PROTECTED MODE FOR THAT ZONE.
        # IN THE CODE BELOW THAT VALUE IS WITHIN THE "SetValueEx" FUNCTION AT THE END AFTER "REG_DWORD".
        #os.system("taskkill /F /IM iexplore.exe")
        try:
            keyVal = r'Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\1'
            key = OpenKey(HKEY_CURRENT_USER, keyVal, 0, KEY_ALL_ACCESS)
            SetValueEx(key, "2500", 0, REG_DWORD, 0)
            print("enabled protected mode")
        except Exception:
            print("failed to enable protected mode")
Dinesh Pundkar
  • 4,160
  • 1
  • 23
  • 37
West
  • 21
  • 1
  • Thank you, this was the best option for me. As long as the edits can be made without special user rights, this is a great turnkey solution. I don't have admin rights on my PC, and it still worked. I would like to add that it's a good practice to use CloseKey(key) at the end to close up the registry key from the process. – eliteproxy Jul 21 '18 at 03:38
2

In case capabilities mode does not work, there is an alternative.

from selenium import webdriver
from selenium.webdriver.ie.options import Options

ie_options = Options()
ie_options.ignore_protected_mode_settings = True
driver = webdriver.Ie(options=ie_options)
driver.get('http://www.google.com')
Vivek Naik
  • 21
  • 2
0

Here is another variation on Dinesh's code that works to disable protected modes in the registry. It also closes the connection.

Simply put this code before your selenium automation code, and it will prepare the browser.

import winreg

def set_reg(REG_PATH, name, value):
    try:
        winreg.CreateKey(winreg.HKEY_CURRENT_USER, REG_PATH)
        with winreg.OpenKey(winreg.HKEY_CURRENT_USER, REG_PATH, 0, winreg.KEY_WRITE) as registry_key:
            winreg.SetValueEx(registry_key, name, 0, winreg.REG_DWORD, value)
            winreg.CloseKey(registry_key)
            return True
    except WindowsError:
        return False

for i in range(1,5): set_reg(r"Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\\" + str(i),'2500', 3)

Adapted from the top answer in this thread by Mash and icc97: python script to read and write a path to registry