After completion of my Selenium with Python script in my local machine, I faced various problems to run it in the server. I didn't find any step by step documentation to run it in the server properly. So I have decided to writing this article based on the problems I have faced and how I came with the solution.
In my script, I was working with Firefox and geckodriver. I also used python 3.7. So the steps you will see below is actually the journey I go through while solving the issues I faced to run it. I will also give the references of the solutions where I found them. So let's begin the journey.
10 Steps to Run Selenium with Python in the Server:
At first login in your server, then work as the instructions given below. You may skip some step if you already have them or may not need them (like installing python 3.7).
1. Install the Python 3.7:
Here is a guide on How to Install Python 3.7 on Ubuntu 18.04 from the Linuxize.
sudo apt update sudo apt install software-properties-common sudo add-apt-repository ppa:deadsnakes/ppa sudo apt install python3.7
If you get any error then try to repeat 1st one and then 4th one.
2. Install pip:
To install Virtualenv and Selenium, we will need pip. So we need to get pip first.
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py sudo python get-pip.py
3. Install virtualenv:
Now install the Virtualenv. Here is a guide on how to create a Python Virtual Environment.
sudo pip install virtualenv
4. Create Virtual Environment:
Now you have to create a virtual environment (here named as "customEnv"). Here is a guide on how to use different Python version with virtualenv from the Stackoverflow.
virtualenv --no-site-packages -p python3.7 customEnv
5. Activate the Virtual Environment:
It is time to activate the virtual environment with following command-
6. Install xvfb:
To run Firefox in server we will need xvfb. So we need to install it. Here is a guide detailing the process.
apt-get install xvfb firefox
7. Install Selenium:
Now install the Selenium.
pip install selenium
8. Install Java:
For selenium to work we will need java to be present. So you can skip it, if you already have it. Here is a guide on How To Install Java with
apt on Ubuntu 18.04.
sudo apt install default-jre
9. Install geckodriver:
- Download the geckodriver.
- Unzip it.
- Copy geckodriver to proper python folder. (In this case ‘/customEnv/bin/geckodriver’)
10. Run Your Python Script:
Finally, all are set. Now run the script.
xvfb-run python <our_python_script>.py
If you follow the steps describe above, hopefully your script will run without any issues. This is a proven steps to run Selenium with Python using Firefox and geckodriver.
Please, feel free to comment below if you face any problem. We will try our best to help you solving your issues. If you are a Python nerd, read our another article on Python Executable for Windows Using Pyinstaller.
Happy Coding ... 😊