In this Python tutorial, we will walk you through the Python Selenium library and perform automated browser testing with Edge and Selenium in Python. If you are using the latest version of Windows, then there is a high chance that Microsoft Edge is installed in your system.
The Edge browser is one of the most popular browsers, and it has 7.75% of the browser market share, which makes it the second most popular web browser. The first position, of course, is occupied by Chrome with 69.28% of the entire browser market share to its own.
So, if you are developing a web application using Django, Flask, or any other popular Python framework , you might want to check or test your web application on the edge web browser for browser compatibility. While testing your web application on Edge, you should be looking for the render quality and accuracy of the page on the browser.
For instance, if you have built a web application and used some CSS and HTML that is running smoothly in Chrome, but you are not sure how it will look on the Edge browser, you can check it with Selenium.
Python comes with a standard unit testing library for code testing, but if you wish to test your web application running on a browser, you need Python automation testing. With automation testing, you can test how the web application is running on different browsers.
But, before diving into the Python code, let's download and install the libraries and web drivers we require for automation testing with Selenium and Python.
Automated Browser Testing with Edge and Selenium in Python
Download Edge Browser and Check Version
If you are a Windows user, Edge will most probably be installed in your system. Nonetheless, you can download the Edge browser from the official website of Microsoft Edge.
After downloading and installing the Edge browser, open it and check your Edge version. To check the Edge version, type edge://settings/help in the URL bar of your Edge browser or click on the Menu --> Help and Feedback ----> Help button.
After visiting the
URL on Edge browser, you will see a similar screen to the one shown below.
As you can see, our Edge version is 87.0.664.75 (Official Build) (64-bit) . Checking the Edge version is important for downloading the corresponding Edge driver for Selenium.
Download Selenium Edge WebDriver
After installing the Edge browser, we need to install the Edge web driver for Selenium. Because we need some middleware that can connect the Selenium Python script to the Edge web browser, you can not perform automation without a web driver.
You can download the compatible web driver from this link . Before downloading the driver zip file, check your Edge version and download the corresponding compatible version of the Edge web driver. Our Edge version is 87.0.664.75 64bit , so we will be downloading the corresponding Edge web driver.
After downloading the edgedriver_win64.zip file, extract it, and you will get the msedgedriver.exe web driver.
Install Selenium Framework for Python
Now you need to install the Selenium browser for your Python environment. And it can be easily downloaded using the Python pip terminal command.
pip install selenium
After the successful installation of Selenium, open your best Python IDE and text editor and start coding.
Write and Run First Automated Browser Testing Script with Python and Selenium
Now, let's write our first browser automation testing script. We will start with connecting the Selenium framework with the Edge browser. Then, we'll proceed with automating the browser and opening techgeekbuzz.com.
# importing selenium webdriver module from selenium import webdriver import time def main(): #initialize the edge web browser edge_browser = webdriver.Edge(r"C:\Users\tsmehra\Desktop\code\msedgedriver.exe") #open techgeekbuzz.com edge_browser.get('https://www.techgeekbuzz.com') #close the browser after 20 seconds time.sleep(20) edge_browser.close() if __name__ == '__main__': #call main funtion main()
We have defined the
class to make the program modular. In the above program, you can see that first, we imported the Selenium
module that will connect to the Edge web driver we had installed.
We have provided the absolute path
of the web driver, but if you wish, you could also provide the relative path.
When you execute the above program, your Edge browser window will pop up, and you will see a similar screen:
When you run the Selenium script, you will also see a message bar saying, "Microsoft Edge is being controlled by automated test software." This shows that your browser is automated by the Python script.
In the above code, we have also mentioned a time limit of 20 seconds for the existence of the automated Edge window. The
method will close the Edge window after the 20-second delay of
Performing Automated Browser Testing with Edge and Selenium in Python Using Web Selectors
Next, let's move further and perform some automation with web selectors. By now, we know how to use Selenium, web driver, and Python to automate the Edge browser and open a web page on it.
Now, let's say we wish to click on the link or button on the page or search for some specific keyword in the search bar of the web application. Let's continue with the homepage of techgeekbuzz.com and enter C++ in the search bar , and hit the search button.
When it comes to clicking a link or filling data in a form with automation browser testing, we need to select the web locator or HTML elements with the help of their tag or attribute name.
For our example, we will be selecting the web locators by their element attribute name, and this can be done with the Selenium driver
method. But before that, we need to know the name for the search
so let's inspect the TGB homepage.
Inspecting the Homepage
By inspecting the homepage, we found out that our search form has
as the name.
Though the search button does not have a name attribute, it has an ID attribute
. So for selecting the search form, we will use the
method and for the search button, we will use the
Next, let's code.
# importing selenium webdriver module from selenium import webdriver from selenium.webdriver.common.keys import Keys import time def main(): #initialize the edge web browser edge_browser = webdriver.Edge(r"C:\Users\tsmehra\Desktop\code\msedgedriver.exe") #open techgeekbuzz.com edge_browser.get('https://www.techgeekbuzz.com') #2 seconds time delay time.sleep(2) #select the search form form_input = edge_browser.find_element_by_name("s") search_button = edge_browser.find_element_by_id("searchsubmit") #clear placeholder value or data form_input.clear() #fill C++ in form form_input.send_keys("C++") #2 seconds time delay time.sleep(2) #hit the search button search_button.click() #close the browser after 20 seconds time.sleep(20) edge_browser.close() if __name__ == '__main__': #call main funtion main()
statement selects the form element of the home page and the
statement selects the search button.
is generally used to clear the placeholder value data. In our case, there was no placeholder. Still, we used it as doing so is a good practice.
statement fills the form with "C++." Between filling the data and hitting the search button, we have specified a 2-second delay with the
At last, with the
statement, we clicked the search button. When you run the above code, you will see a similar output like below:
In this Python tutorial, we learned about automated browser testing with Edge and Selenium in Python. Apart from Edge, we can also perform browser automation with Chrome, Firefox, and Safari. In this tutorial, we filled out the form and hit the search button to automate the search quality of our web page. Similarly, we can perform an automation login by filling the login form with login credentials.
Check out the Python tutorial on how to automate login with Selenium and Python to know more.
Here, we have automated Facebook and GitHub login with Python and selenium. The Selenium web framework supports many inbuilt functions and methods. You can also read the official documentation of Selenium to check out all the amazing inbuilt features.
People are also reading: