Automated Browser Testing with Edge and Selenium in Python

Learn how to perform automated browser testing with Microsoft Edge browser and Selenium library in Python.
  Guest Contributor · 8 min read · Updated may 2024 · Web Scraping

Unlock the secrets of your code with our AI-powered Code Explainer. Take a look!

As of September 2020, Microsoft Edge holds 8.84% of the browser market and stands at the second position after Chrome. But still, while performing cross-browser testing, our main focus tends to revolve primarily around browsers like Chrome, Firefox, or Safari. But what about other browsers, like Edge?

While testing a website, it is quite important that it is rendered smoothly and accurately over all browsers, devices, and machines. Suppose you created a website and tested it on browsers like Chrome, Firefox, and Safari manually and decided to deploy it. But what if the users open the website in Microsoft Edge or in a version of browsers other than the tested ones?

Therefore, it is quite important to perform Automated Browser Testing to test your website across multiple browser-version combinations so as to ensure an efficient user experience with the help of Selenium. In this tutorial, we will dive in-depth on how to perform automated browser testing with Edge and Selenium in Python. We will be using the Edge WebDriver to help us run our Selenium automation test scripts over the Edge browser.

Download Selenium Edge WebDriver

While performing automated browser testing, it is important for the test scripts to communicate with the browser. Without having a WebDriver, this might not be possible. The Selenium Edge WebDriver works as the mediator between your code and the browser, which helps to provide the programmable remote control of the browser.

You can download the latest version of Selenium Edge WebDriver using this link.

Performing Browser Automation With Edge And Selenium In Python

Let us see what are the prerequisites to use Edge with Selenium and Python for browser automation:

  • Download the latest version of Python, if not already installed, from here.
  • Next, we need the Microsoft Edge browser. You can use this link to download it. Please note that the version you are downloading is for WebDriver purposes. If you have Edge already installed, you can find out its version by typing the below command in Edge’s address bar:
    • edge://version/
  • You can also find out the version of your Edge browser using the below steps:
    • Open the Microsoft Edge browser.
    • Select Settings and more at the top right of the browser and go to Settings.
    • You can see a section about Microsoft Edge at the bottom of the settings tab that contains information about your Edge version.

Now that the basic setup is ready, let's proceed to the next steps to download and install the following:

  • Selenium framework for Python – Execute the below command in the terminal after you have already installed Python language to install the latest version of Selenium framework for Python language:
pip3 install selenium
  • Edge WebDriver for Selenium – Download the Edge WebDriver executable from this link that matches your system’s configuration. Make sure to download the driver version based on the version of Edge installed in your system. Unzip the file and copy the location of the msedgedriver.exe.
  • Selenium tools for Microsoft Edge – Execute the below command from the terminal to download the Selenium tools for Microsoft Edge directly:
pip install msedge-selenium-tools selenium==3.141

We are all set. Now, we can perform automation testing with Edge and Selenium in Python. Let us see some examples.

Running Your First Automated Browser Testing Script

In this example, we will see how to write the first script that initiates the automated browser testing with Edge and Selenium:

# importing required package of webdriver
from selenium import webdriver
# Just Run this to execute the below script
if __name__ == '__main__':
   # Instantiate the webdriver with the executable location of MS Edge web driver
   browser = webdriver.Edge(r"C:\Users\LenovoE14\Downloads\edgedriver\msedgedriver.exe")
   # Simply just open a new Edge browser and go to lambdatest.com
   browser.get('https://www.lambdatest.com')

In this code above, we are just creating the WebDriver instance for Microsoft Edge by passing the path of the executable Edge WebDriver as a parameter. In this case, it is C:\Users\LenovoE14\Downloads\edgedriver\msedgedriver.exe. Then, we open a new Edge instance with the provided URL. You can customize the URL to your liking.

Execute the above code, and you will see an instance of the Microsoft Edge getting started with 'https://www.lambdatest.com' in its address bar (because we have provided this address to the web driver instance and asked the browser to open it):

First script executed

Performing Browser Automation Using Web Locators

Let us now look at another example, to perform some specific actions with the help of web locators. In this example, we will try to insert the email address at the home page of www.lambdatest.com and click on the button Start Free Testing, as shown in the image below:

Start free testingCopy and paste the below code in your IDE/Text editor and run it:

# importing required package of webdriver
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from time import sleep
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.opera.options import Options
from selenium.webdriver.support.wait import WebDriverWait

if __name__ == '__main__':
    # Instantiate the webdriver with the executable location of MS Edge
    browser = webdriver.Edge(r"C:\Users\LenovoE14\Downloads\edgedriver\msedgedriver.exe")
    # Simply just open a new Edge browser and go to lambdatest.com
    browser.maximize_window()
    browser.get('https://www.lambdatest.com')
    try:
        # Get the text box to insert Email using selector ID
        myElem_1 = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID, 'useremail')))
        # Entering the email address
        myElem_1.send_keys("rishabhps@lambdatest.com")
        myElem_1.click()
        # Get the Submit button to click and start free testing using selector CSS_SELECTOR
        myElem_2 = WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#testing_form > div")))
        # Starting free testing on LambdaTest
        myElem_2.click()
        sleep(10)
    except TimeoutException:
        print("No element found")
    sleep(10)
    browser.close()

In the code above, we have inserted the email address in the text box shown above and pressed the button Start Free Testing. We have used the selectors ID and CSS SELECTOR for this. Upon execution, a new instance of Microsoft Edge will open and perform the following actions in the given order:

A new instance of Microsoft Edge will be launched:

Microsoft Edge instance launched using SeleniumThe browser will get maximized (as we execute the browser.maximize_window()):

Browser Maximized

The LambdaText homepage will open:

Home page opened

Given email will be inserted in the text box, and the button Start Free Testing will be pressed:

Email inserted

You'll be redirected to the login page to set up your account and start your cross-browser testing with the help of LambdaText:

Redirected to Login PageConclusion

In this article, we have successfully seen:

  • What is Edge WebDriver, and how do you get it.
  • How do you set up automated browser testing with Edge and Selenium in Python.
  • Writing the first browser automated test.
  • Using Selectors to perform browser automation with Edge and Selenium in Python.

The popularity of Microsoft Edge is constantly rising. We have performed the browser automation using an installed version. But what if we need to perform this in another version or another Windows OS version?

In such a scenario, downloading and testing each version might not be possible, thereby leading to certain drawbacks or some user experience issues later. To overcome this, we can use LambdaTest for cross-browser testing. LambdaTest will not only allow you to test your website on multiple browsers and operating system combinations but will allow you to perform parallel testing as well on cloud Selenium Grid.

LambdaTest provides a platform to perform live interactive browser compatibility testing of your public or locally hosted websites and web apps on 2000+ real mobile and desktop browsers running on real operating systems.

Learn also: How to Use Proxies to Rotate IP Addresses in Python.

Happy testing ♥

Take the stress out of learning Python. Meet our Python Code Assistant – your new coding buddy. Give it a whirl!

View Full Code Generate Python Code
Sharing is caring!



Read Also



Comment panel

    Got a coding query or need some guidance before you comment? Check out this Python Code Assistant for expert advice and handy tips. It's like having a coding tutor right in your fingertips!