How to use Selenium with Internet Explorer

Introduction

(Note: This guide is focused on Python but may be useful for other languages)

Do you want to use Selenium to manipulate/control Internet Explorer? OK, here’s the first tip:

  • Don’t.

Another really good tip is:

  • Seriously, don’t. Go use chrome or something. Why are you doing this to yourself?

But i have to! The thing i’m trying to do ONLY works on IE!“, you scream in despair. An angel weeps, for he knows your soul shall not escape unharmed.

Below are some tips to follow when using selenium with IE. Please note that, even though they are tips, you should try to follow all of them. Not doing so may cause bugs, loss of deadlines and frustration.

Okay, here are the actual tips:

1 – Make sure the computer is configured correctly!

Unfortunately, IE has a lot of configuration issues with Selenium. If you don’t configure IE correctly you will have unstable tests with random crashes everywhere.

Here are the really important ones, taken directly from Selenium’s github page:

1.1 – Protected mode settings must match for each zone.

  • On IE 7 or higher on Windows Vista or Windows 7, you must set the Protected Mode settings for each zone to be the same value. The value can be on or off, as long as it is the same for every zone. To set the Protected Mode settings, choose “Internet Options…” from the Tools menu, and click on the Security tab. For each zone, there will be a check box at the bottom of the tab labeled “Enable Protected Mode”.

^ Really really really important. The driver will break when you jump to URLs in a different “security zone”. As the description says, you can choose any security settings, but please try to choose the most secure one.

1.2 – Enhanced Protected Mode must be disabled for IE > 10.

  • Additionally, “Enhanced Protected Mode” must be disabled for IE 10 and higher. This option is found in the Advanced tab of the Internet Options dialog.

^ Also really important. If you want/need to change this via the registry, the key is as follows:

Registry Hive HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER
Registry Path Software\Policies\Microsoft\Internet Explorer\Main
Value Name Isolation
Value Type REG_SZ
Enabled Value PMEM
Disabled Value PMIL

From what i understand, this setting prevents the driver from hooking IE’s process. The driver simply won’t work if this is enabled. But i could be wrong.

1.3 – Other stuff that doesn’t really need an explanation.

  • The browser zoom level must be set to 100% so that the native mouse events can be set to the correct coordinates.
  • For Windows 10, you also need to set “Change the size of text, apps, and other items” to 100% in display settings.
  • For IE 11 only, you will need to set a registry entry on the target computer so that the driver can maintain a connection to the instance of Internet Explorer it creates. For 32-bit Windows installations, the key you must examine in the registry editor is HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BFCACHE. For 64-bit Windows installations, the key is HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BFCACHE. Please note that the FEATURE_BFCACHE subkey may or may not be present, and should be created if it is not present. Important: Inside this key, create a DWORD value named iexplore.exe with the value of 0.

Nuff’ said. But, just to be very very clear: Selenium absolutely won’t work if you don’t configure things properly. You have been warned.

2 – Never never ever use “sleep(x)”

Your script needs to enter a page and wait for something to appear. You think to yourself: “Okay, a 3-second sleep will do”. The selenium website recommends using implicit or explicit waits , but alas you sin once more and think: “whatever, just sleeping will work, i don’t need the extra efficiency”.

Well, here’s the problem: Sleep commands break the driver. 

I believe that since the driver actually works as a server/client pair, with your script basically only starting the driver and then sending commands, using sleep commands halts the entire program and may interfere with this connection. In my own experience, i had to switch every single sleep command for explicit waits because simple commands (such as going to an URL or something) were failing after sleeps. The errors were usually something like “Unable to connect to driver” .

ALWAYS use explicit waits. Implicit waits, in my experience, don’t work that well. 

You can learn more about Selenium’s explicit waits here.

3 – Be careful with iframes.

HTML code inside an iframe will not be readily recognizable by Selenium. You have to switch to the iframe first and THEN do what you have to do. 

And of course, if you want to manipulate HTML outside the iframe, you will have to switch OUT of it before continuing.

If you don’t, Selenium will be unable to find elements inside the iframe and throw “object not found” errors at you.

Having problems finding elements? Make sure the elements aren’t inside an iframe!

Also, if you have nested iframes in a page, you will have to switch to each one in succession.

4 – Not every pop-up is a pop-up.

Selenium has functions for accepting and rejecting pop-ups, but sometimes a pop-up isn’t a popup, it’s a floating something or a new window in modal mode that always grabs the focus.

The best way to deal with these kinds of things is to wait until the new window opens, switch to it, do what you have to do, close it, then switch to the original window again.

You can use this to wait until a new window opens:
WebDriverWait(driver, 5).until(EC.number_of_windows_to_be(2))

If you know a window is going to appear, be sure to save the original window’s handle. You will need it when switching back. New windows aren’t always added to the end of the “current windows” array. Sometimes they are added to the 0th position and push everything else to the right. Sometimes they are added to the last position and don’t push anyone.  That’s why you should store the original window handle: you can’t know which is the “original” window after a new one appears.

5 – Don’t “spam” commands.

If you ask Selenium to list all current window handles every 0.1s Selenium will crash. Again, i think this has to do with the server/client connection Selenium uses, and “spamming” commands may break that connection or overload it somehow.

If you need to repeatedly call a Selenium function you should probably be using an explicit wait. If you really really have to, be sure to include a long enough wait time. You can use sleep(x) to do this if it isn’t a function that directly manipulates the browser (i.e., alters the state of Internet Explorer).

6 – Need to debug a modal window? It’s not that easy :,(

Modal windows in IE (those windows that pop-up without address bars) aren’t that easy to debug. You can’t just press F12 to open the developer’s menu.

If you need to debug a modal window, follow the tips posted on this Stackoverflow question.

There are many methods available. One should suit you and your company’s security settings.

 

Thank you for reading.

I hope this guide was useful to you. If you have any questions feel free to leave a comment below!

Leave a Reply