How to Open IE Using Selenium Webdriver?

Selenium WebDriver is one of the most popular browser automation tools used by software testers and developers. While Selenium supports all major browsers like Chrome, Firefox, Safari etc, you may sometimes need to automate Internet Explorer for testing compatibility of your web application.

In this comprehensive 1500+ words tutorial, I will provide expert tips and step-by-step instructions on setting up IE WebDriver in Python from scratch. By the end, you will have the knowledge to write reliable automation scripts for Internet Explorer 11 and Edge with Selenium.

Introduction to Selenium WebDriver

Selenium WebDriver is an open-source API that allows programmatic control of a browser through simple commands in languages like Python, Java, C#, etc. The WebDriver protocols helps Selenium interact with a browser natively like a real user would.

Some key capabilities offered by Selenium WebDriver include:

  • Launching browsers and navigating to URLs
  • Finding web elements and interacting with them (click, type text, clear values etc.)
  • Executing JavaScript in the browser context
  • Assertions to validate page state
  • Taking screenshots of pages
  • Headless execution without a UI (useful for CI/CD testing)

Brief History of Selenium Project

Selenium was created by Jason Huggins in 2004 as an internal tool at ThoughtWorks for testing their own applications. The name comes from the chemical Selenium which was used to “inhibit oxidation” or applied to slow down early browsers like IE.

Selenium 1 first implemented the Selenium Core JS framework allowing scripted actions in the browser. Selenium 2 debuted WebDriver protocol for direct browser control.

The latest Selenium 3 combined WebDriver APIs with multi-browser support, distributed testing capabilities and integration with frameworks like JUnit, NUnit, PyTest etc.

Selenium Architecture

Selenium WebDriver architecture consists of four major components:

  • Selenium Client libraries¬†like Selenium WebDriver API, Selenium Grid – Help initiate test execution
  • Browser specific drivers¬†like ChromeDriver, IEDriverServer.exe – Implements the WebDriver wire protocol for specific browsers
  • Browsers¬†like Chrome, Firefox, IE – Executes the tests
  • ** Selenium server** – Allows distributed test execution across multiple machines

This architecture provides a browser and language neutral interface for writing reliable test automation.

Why Automate Internet Explorer?

Although Internet Explorer has been replaced by Microsoft Edge as the default browser in Windows 10, you may still need to automate and test IE11 for compatibility of public websites or internal enterprise apps that have IE-specific code.

Here are some key reasons why testing with Internet Explorer WebDriver is still relevant:

  • Legacy App Support: Lots of in-house tools and apps built years ago may only work properly on IE due to ActiveX, VBScript dependencies. Running automated UI tests with IE can prevent regressions.
  • Compatibility Testing: To ensure your public website works fine across browsers, you need to run UI tests on IE along with Chrome, Firefox etc.
  • Accessibility Testing: IE11 is still used by people for better accessibility support through features like pre-IE11 document modes. Automated testing helps ensure your site is usable for them.
  • Market Share: Though declining, IE11 still has significant market share of around 5% globally as per Statcounter stats. Running UI tests on IE can prevent issues for these users.

However, testing with IE poses some challenges like dealing with security prompts, compatibility modes, frequent DOM changes compared to other browsers. In later sections, I will share tips to handle these issues.

Prerequisites for IE WebDriver

Before you can start automating Internet Explorer with Selenium, you need to have the following set up:

Python Environment

Since we will be using Python binding of Selenium, install Python 3.6+ from python.org along with Pip package manager.

Selenium Python Package

Run pip install selenium to install the Selenium 3 Python package. This will allow creating WebDriver instances in Python code.

Microsoft Visual Studio

The IE WebDriver is a .Net assembly that requires Visual Studio libraries to run. Install Microsoft Visual Studio 2019 or Build Tools for Visual Studio from https://visualstudio.microsoft.com to get required C++ packages.

IE Driver Executable

Download the IE Driver Server executable for your IE browser version from https://selenium.dev/downloads/. Make sure the IE Driver version matches browser version, eg IE Driver 3.150.1 for IE 11.

Some options for IE browser drivers:

  • IEDriverServer.exe – Legacy IE driver for older versions
  • IE Driver – New driver for IE 10 onwards
  • IE Browser Driver – Dev preview driver for IE and Edge

Choose as per your specific IE browser version and automation needs.

Enable Protected Mode Settings

IE by default runs in protected mode which prevents automation. So you need to disable protected mode for all zones by going to Internet Options > Security.

This will allow IE WebDriver to work properly. For Chrome, Firefox etc protected mode doesn't need to be disabled.

Launching IE with WebDriver in Python

Now that we have set up the prerequisites, let's see sample Python code to launch Internet Explorer using Selenium:

from selenium import webdriver

IE_DRIVER_PATH = './iedriver.exe' 

driver = webdriver.Ie(IE_DRIVER_PATH)
driver.get('http://www.google.com')

Let's understand what's happening here:

  1. We import webdriver classes from the selenium Python package
  2. Specify path to the iedriver.exe file we downloaded earlier
  3. Create a new Ie WebDriver instance by passing the driver path
  4. Use WebDriver's get() method to launch a URL like http://google.com

This will open up a new IE browser and navigate to Google!

Finding and Interacting with Elements

Once the browser is launched, next step is locating elements on the page and interacting with them as required in your test.

Some common operations like entering text in a textbox can be done as:

search_box = driver.find_element(By.NAME, 'q')
search_box.send_keys('Automate IE with Selenium Python') 
search_box.submit()

Here we find the search input box by its name attribute, enter text using send_keys(), and submit the form using submit().

There are many other methods like click(), get_attribute(), is_displayed() etc. to interact with elements on a page.

Some useful locator strategies for IE:

  • ID – Unique, reliable locator
  • Name – Can identify form elements
  • Link text – Locate links and buttons
  • CSS Selectors – Flexible, lots of matching options
  • XPath – Powerful but prone to issues in IE DOM

Avoid positional locators like WebDriver's default index-based location. These are brittle prone to breakage.

Executing JavaScript in IE

Selenium also provides methods to directly execute JavaScript in the browser context. This is useful as IE has some quirks which can only be handled reliably through JS.

For example, to scroll down on a page:

driver.execute_script('window.scrollTo(0, 500)')

We can also get properties of elements using getElementById and related JS functions:

search_box = driver.execute_script('return document.getElementById("search_form_input")')

This will return the search box web element without needing prior element location.

Some other common use cases of JS execution with IE WebDriver:

  • Scroll element into view for interaction
  • Trigger events like click, focus, blur etc.
  • Get properties like innerHTML, textContent
  • Set values for non-input elements
  • Handle browser dialogs like alert(), confirm()

Setting Up Automation Framework

To organize IE WebDriver tests better, we should use a test framework like pytest or unittest rather than writing scripts from scratch.

This helps with:

  • Fixture methods for setup/teardown
  • Parameterization of tests
  • Categorization using markers
  • Reporting integrations
  • Parallel execution

Here is an example IE test in pytest:

import pytest
from selenium import webdriver

@pytest.fixture(scope="session")
def driver():
   ie_driver = webdriver.Ie()
   yield ie_driver
   ie_driver.quit()
   
def test_ie_google_search(driver):
   driver.get("https://google.com")
   # Rest of test steps

The fixture initializes the IE driver and closes it after tests finish. We can parameterize driver across IE, Chrome etc. for multi-browser testing.

Dealing with IE Security Prompts

One common problem while automating IE is dealing with security prompts for trusted sites, file download etc. These can interrupt WebDriver test execution.

To avoid such prompts, you need to adjust IE security settings to properly allow automation. Some useful settings are:

  • Add your test site URLs or domains as trusted sites
  • Enable downloads by disabling notification prompts
  • Allow protected mode for trusted sites only, not for all zones
  • Disable Enhanced Protected Mode which further restricts automation
  • Increase Script Execution Delay to prevent timeout errors

Also make sure your IE security level is not too high, set it to Medium or Medium High.

Programmatic Options to Handle Security Prompts

We can also handle security prompts in code using _ IEOptions _ class:

options = webdriver.IeOptions() 
options.ignore_protected_mode_settings = True
options.ignore_zoom_level = True

driver = webdriver.Ie(options=options)

This disables protected mode checks and zoom level warnings.

Handling IE Compatibility View

IE has a compatibility view to make sites work properly which are designed for older IE versions.

This can cause issues for automation like changed DOM. Make sure compatibility view is NOT enabled for your test sites.

You can also use the WebDriver API to programmatically enable/disable compatibility view for a site:

from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

caps = DesiredCapabilities.INTERNETEXPLORER.copy()
caps['ignoreProtectedModeSettings'] = True    
caps['requireWindowFocus'] = True       
caps['nativeEvents'] = True 

driver = webdriver.Ie(desired_capabilities=caps)

Pass the capabilties during driver initialization to configure compatibility view settings.

Additional Tips to Handle IE Quirks

  • Use edge mode rather than legacy IE document modes for stable DOM
  • Disable auto-redirects which can cause session issues
  • Adjust zoom level programmatically to override default IE zoom
  • Disable popup blockers that obstruct automation
  • Add site to Local intranet zone to reduce security prompts
  • Handle certificate errors and accept untrusted certs if needed

Locating Dynamic Elements in IE

A major challenge with IE automation is dealing with dynamically changing DOM elements.

Here are some strategies to handle this:

  1. Use explicit waits instead of sleeps to wait for elements to appear before interacting
  2. Prefer CSS selectors over XPath to locate elements which are less prone to changes
  3. Use JavaScript methods like document.getElementById as fallback locators if needed
  4. The page object pattern also helps makes tests more resilient to UI changes
  5. Adopt a smart wait strategy with configurable timeouts

Troubleshooting Common IE WebDriver Issues

Here are some common issues faced while using IE WebDriver and how to troubleshoot them:

  • IE launch is slow: Launch IE driver using headless mode to avoid delays in UI startup and shutdown.
  • Element not found: Use WebDriverWait and expected conditions to retry finding elements.
  • Element covered by overlay: Use JavaScript to click overlay and dismiss it.
  • Site not working in IE mode: Set IE document mode to IE11 using WebDriver capabilities.
  • IE hangs or crashes: Disable native events and use JS only execution model to stabilize execution.
  • Driver version mismatch: Ensure IE driver version matches the IE browser version.
  • 32 bit vs 64 bit conflicts: Use matching bitness for IE, Python, WebDriver and OS (32 or 64 bit all).
  • Protected mode errors: Add sites as trusted and disable enhanced protected mode.
  • Stale element exceptions: Prefer page object model usage to avoid stale elements.

Integrating with CI/CD Pipelines

To leverage the benefits of continuous testing, we should integrate our IE WebDriver scripts with CI/CD platforms like Jenkins, TeamCity, CircleCI etc.

Some options:

  • Run IE tests on a Windows node in the CI/CD pipeline using parallel stages
  • Execute IE tests on Selenium Grid hub linked to CI/CD for distributed testing
  • Use Docker images with IE and WebDriver preconfigured for easier setup
  • Configure IE WebDriver capabilities for headless execution without UI for CI/CD
  • Set up automated pipeline triggers to execute tests on every commit or schedule
  • Publish Selenium test results and logs from the CI/CD system

Recommended Tools To Enhance IE Test Automation

Along with WebDriver, some other useful tools and frameworks for boosting IE test automation are:

  • Sizzle.js¬†– Alternative locator engine with more flexible queries
  • Appium¬†– Supports IE mobile emulation using Windows device virtualization
  • Sauce Labs¬†– Cross-browser cloud testing platform with thousands of IE browser configs
  • LambdaTest¬†– Scalable cloud based Selenium Grid for IE test parallelization
  • BrowserStack¬†– Features comprehensive IE browser coverage on cloud VMs
  • TestingBot¬†– Selenium cloud platform with support for testing legacy IE versions

Best Practices for IE Test Automation

Here are some best practices I recommend based on experience for stable, reliable IE test automation:

  • Adopt a¬†page object model¬†for improved test maintenance
  • Use¬†explicit waits¬†instead of sleeps to handle dynamic elements
  • Parameterize¬†test data to avoid hard-coded values
  • Create ** reusable utility methods** for common test actions
  • Take¬†reference screenshots¬†to capture expected page states
  • Ensure¬†cross-browser compatibility¬†in CSS selectors
  • Run tests on¬†multiple IE versions¬†like IE11, IE10 etc.
  • Leverage¬†headless execution¬†for faster test runs
  • Enable¬†logging¬†and screenshots to debug failures
  • Perform¬†multi-threaded test execution¬†for efficiency

FAQs on Internet Explorer Test Automation

Here I have compiled some frequently asked questions on automating IE browser using Selenium:

How do I enable protected mode for automation in IE?

You need to disable protected mode for all zones under Internet Options > Security. For other browsers, protected mode doesn't need changing.

What is the difference between IE Driver Server and IEDriverServer.exe?

IE Driver Server is the new IE browser driver for IE 10+ versions. IEDriverServer.exe is the old legacy IE driver for older IE versions.

How can I fix security certificate errors in IE during automation?

You can add a capability like ignoreProtectedModeSettings to ignore cert errors or accept untrusted certificates programmatically.

What is the best element location strategy for IE browsers?

ID and CSS selectors work most reliably across IE versions. XPath has good features but is prone to issues due to IE's DOM handling.

How do I execute IE WebDriver tests on Selenium Grid?

You can configure RemoteWebDriver to connect to Grid hub URL. Then tests will run distributed across Grid nodes.

Conclusion

I hope this gives you a good understanding of how to automate UI testing for Internet Explorer browsers using Selenium WebDriver in Python. Let me know if you have any other topics you would like me to cover in IE browser test automation.

John Rooney

John Rooney

John Watson Rooney, a self-taught Python developer and content creator with a focus on web scraping, APIs, and automation. I love sharing my knowledge and expertise through my YouTube channel, My channel caters to all levels of developers, from beginners looking to get started in web scraping to experienced programmers seeking to advance their skills with modern techniques. I have worked in the e-commerce sector for many years, gaining extensive real-world experience in data handling, API integrations, and project management. I am passionate about teaching others and simplifying complex concepts to make them more accessible to a wider audience. In addition to my YouTube channel, I also maintain a personal website where I share my coding projects and other related content.

We will be happy to hear your thoughts

      Leave a reply

      Proxy-Zone
      Compare items
      • Total (0)
      Compare
      0