How to Open and Control the Firefox Web Browser in Windows using the Selenium Module in Python



Python


In this article, we show how to open and control the Firefox web browser in Windows using the Selenium Module in Python.

Using certain tools within the Selenium module in Python, we can open and control the Firefox web browser through Python code.

Instead of the normal, conventional way of clicking the Firefox web browser and manually entering in a URL to get to a web page, we can achieve all of this through Python code.

In this article, we show how to install selenium and geckodriver so that you can make use of this functionality.

So the first thing you need to do is install the selenium module.

This can be done in the windows command prompt using the line below.



When running a pip command, make sure that you're within the Scripts directory of Python.

It will tell you then if it has successfully installed.

You can check if it is installed by opening your Python software and typing in, import selenium

If there is no error message, then the selenium module is installed on your PC.

The next thing we must do is install the geckodriver for our Windows PC.

The geckodriver software allows us to interact with Gecko-based web browsers, of which one is Firefox. The software provides the HTTP API to communicate with these browers.

If you have a 32-bit Windows PC, you can download this software: geckodriver-v0.28.0-win32

If you have a 64-bit Windows PC, you can download this software: geckodriver-v0.28.0-win64

Once you have downloaded this file and extracted it (being that it is a compressed file), then the next thing you have to do is add it to the Windows Path environment variable. This allows Windows to be able to locate the executable file of the program to run it. If you don't add it a path environment variable for it, the Windows operating system wouldn't know where the executable file is to run the program.

To add a path environment variable for the program, go to the Windows search bar at the bottom and type in, Edit Environment Variables.

The following should now be shown on your screen.

Editing path environment variables in Windows

To edit path environment variables, click on 'Path' and click the 'Edit' button.

The following should now be shown on your screen.

Adding a new path environment variable for geckodriver in Windows

Now that you're on this Screen, click 'Add' to put in another path variable. Add the complete path to the folder which contains the executable geckodriver program. You're not specifying the path to the executable file, but t to the folder that contains the executable file.

Once you've clicked 'Ok' twice, there should be a path environment variable to the geckodriver software on your Windows PC.

To test this out, open the Command Prompt on your Windows PC, and type in geckodriver.

If you see the following below, this means that the geckodriver software on your PC is up and running.

Testing the geckodriver installation windows

Once you've reached this point, then you can now open your Python software.

We will write a basic program using the Selenium module to open the Firefox web browser and go to amazon.com website.

This is shown in the code below.



The first thing we have to do is import webdriver from the selenium module. This allows us to work with web browers; in this case, Firefox.

We then create a variable, browser, which we set equal to, webdriver.Firefox(). This allows us to make a connection to the Firefox browser.

Next we use the get() function to retrieve the amazon.com website.

Know that all of this is done by the webdriver in Python code. We do not click the Firefox icon manually to open it up or manually enter in the URL. All of this is done through the webdriver in the Python code.

So this is how we can open and control the firefox web browser in Windows using the selenium module in Python.


Related Resources

How to Create a Zip File in Python

How to Extract All Files and Folders from a Zip File in Python

How to Read the Contents of a Zip File in Python



HTML Comment Box is loading comments...