What are Captive Portals?
If you have ever been to a hotel which provides wifi facilities, when you tried to connect to it, you would be redirected to a web page. This web page is known as Captive Portal.
Captive Portals are used in various public and semi-public spaces for many reasons like access control or usage tracking or just enhancing user experience with fancy looking pages.
Why you may need to automate the process of login?
Some places store the authentication results in persistent storage cookies meaning you will not have to login using captive portal again and again but some places do not use this mechanism.
In these places, you are constantly pestered with these captive portals which gets boring after a point. To provide a solution to this problem, I bring to you a solution which could solve your problems.
Prerequisites
- Python (anything >= 3.7 should work)
- selenium (a python package)
- WebDriver (corresponding to the browser you want to use)
Below are the resources where you can download web drivers from –
1. Firefox
2. Microsoft Edge
3. Opera
4. Chrome (not recommend since it is buggy)
Once you have installed the resources, we can proceed.
The Solution –
The Python Script –
from selenium import webdriver from selenium.webdriver.firefox.service import Service from time import sleep from selenium.webdriver.common.by import By geckodriver_path = "/path/to/your/webdriver" service = Service(executable_path=geckodriver_path) driver = webdriver.Firefox(service=service) try: # Navigate to the login page driver.get("Link of the captive portal") sleep(1) # we are sleeping here to let the site load first # Find and fill out the login form username_field = driver.find_element(By.NAME, "userId") password_field = driver.find_element(By.NAME, "password") login_button = driver.find_element(By.NAME, "Submit") # Enter your credentials username_field.send_keys("enter your username") password_field.send_keys("enter your password") # Click the login button login_button.click() # Optionally, wait for the next page to load and verify the login was successful # sleep(5) finally: # Close the browser driver.quit()
Here I am using geckodriver, the webdriver for firefox but you can replace this with your choice of driver and browser.
This script is for a basic captive portal with a username field, a password field and a Submit button. For your specific usecase you would need to replace “userid”, “password” and “Submit” with the element names for that specific site.
To do that, you can inspect the html code of site using F12 or right click and inspect. Then navigate to the form and get the field and button names.
Great! Now we have the python script setup but it is still not done. We need to form a setup such that it runs automatically duing startup.
Command Line Script –
Below is the command line script for windows operating system, for other operating systems the script would be different.
@echo off REM Change the path to the Python executable if necessary set PYTHON_PATH=C:\path\to\python.exe REM Change the path to the directory containing your Python script set SCRIPT_DIR=C:\path\to\your\script\directory REM Change to the script directory cd /d %SCRIPT_DIR% REM Run the Python script "%PYTHON_PATH%" selenium_script.py REM Pause to see any output pause
Please adjust this script according to your operating system and file paths.
Now we are ready with a command line script. What’s next?
We will the Task Scheduler inbuilt in Windows –
Here’s the formatted list of steps to add a script to Task Scheduler to start at startup:
- Open Task Scheduler:
- Press
Win + R
to open the Run dialog. - Type
taskschd.msc
and pressEnter
. This will open Task Scheduler.
- Press
- Create a Basic Task:
- In the Task Scheduler window, click on “Create Basic Task…” in the Actions pane on the right side.
- Name and Describe the Task:
- Name: Enter a name for your task, such as “Run Selenium Script at Startup.”
- Description: Optionally, add a description for clarity.
- Choose a Trigger:
- Trigger: Select “When the computer starts” and click “Next.”
- Start a Program:
- Action: Choose “Start a program” and click “Next.”
- Select the Batch File:
- Program/Script: Click “Browse…” and navigate to your batch file (
run_selenium_script.bat
). - Arguments (optional): Leave this blank.
- Start in (optional): You can specify the directory where your batch file is located if needed.
- Program/Script: Click “Browse…” and navigate to your batch file (
- Finish the Setup:
- Review the settings and click “Finish.”
Now when you restart your computer you will see that script runs automatically.
This is one of the many applications of selenium, a powerful and widely-used open-source framework for automating web browsers.
If you are interested in reading about it further, here is the link to the docs.
Be First to Comment