Python Friday #149: Multiple Browsers in Selenium Grid

One of the greatest benefits of Selenium Grid is to run Selenium against multiple browsers. This can be multiple instances of one web browser that we run in parallel, or browsers from different vendors. Let’s see how we can configure a flexible solution without much effort.

This post is part of my journey to learn Python. You can find the other parts of this series here. You find the code for this post in my PythonFriday repository on GitHub.

 

Multiple containers

We can use the official browser-specific images and combine them to a flexible grid. However, starting those containers one by one will be a pain. With docker-compose we already have a tool that is great to orchestrate a few containers. All we need to do is to create a docker-compose.yaml file and start it.

 

Create a docker-compose.yaml file

To create a flexible Selenium grid, we need one hub container that coordinates everything and for each browser instance we add a container called a node. The nodes need to know where the hub is and how they can reach it.

This docker-compose.yaml file creates one hub and four nodes (1 for Chrome, 1 for Edge, and 2 for Firefox):

 

Run the grid

Since we use Docker compose, we can use this command to start our grid:

If you go to the hub (http://localhost:4444/ui#), you should see something like this:

There should be 4 nodes waiting

 

Use a specific browser

If we want to run our code against a Firefox browser, we can reuse the code from the last post:

If we want to run with Chrome, we can change it to this:

And for Edge, we can use this code:

Everything else in our code is the same and works for each browser.

 

Next

We can extend our docker-compose.yaml and add more containers as our requirements grow. Next week we look at creating a more dynamic Selenium Grid.

2 thoughts on “Python Friday #149: Multiple Browsers in Selenium Grid”

  1. Hi, have you tried the playwright lib?
    I have recently moved my projects to that lib and I found it much easier to use. as well, it handles lots of brower’s events.
    worth giving it a look.
    Best

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.