This article explains how to change the WLAN (WiFi) password using JavaScript. Previously, the same procedure is successfully performed using Python (see here). The router used in this experiment is the Huawei EG8145V5, which is connected to an internet service provider. If the password change is done manually by accessing the router’s UI, the process can be seen in the video below:
Since the router’s IP address being accessed is a static page, it will be relatively easy to change the password using JavaScript. However, the device used to change the password must be connected to the router’s internet network, firstly. Nevertheless, this method can be used if we want to automate the process in the future.
What’s we need?
From the video above, we can see that changing the WiFi password requires several steps, which are:
- Logging into the router’s main page.
- Accessing the WLAN settings page.
- Changing the password on the form page.
- Clicking the “apply” button.
For these steps, there is a python package called Selenium that can be used. Selenium has features to access web pages, such as filling out forms, clicking buttons, and more. Therefore, we will use Selenium in JavaScript.
The modules to be imported are as follows:
const { Builder, By, Key, until } = require('selenium-webdriver');
const chrome = require('selenium-webdriver/chrome');
const argparse = require('argparse');
We use the Chrome driver. Additionally, the argparse module is used to pass variables that can be input when running the program.
The concept of writing this program is to access the HTML elements. These elements can be accessed using their name, ID, class name, or even XPATH.
The main program is structured in a function as shown below. The output of this function includes the implementation of the steps mentioned above (from the second step to the end) as well as recording the SSID (WiFi) name, the previous password, and the newly set password.
async function changePassword(driver, freq) {
if (freq === '5g') {
// Select Basic 5 GHz Tab
const getFreq = await driver.findElement(By.name('subdiv_WlanBasic5G'));
await getFreq.click();
await driver.sleep(3000); // Wait for the page to load
}
// Switch to the iframe
const iframe = await driver.findElement(By.id('frameContent'));
await driver.switchTo().frame(iframe);
if (freq === '2g') {
// Select Secondary 2.4 GHz Tab
const wifiChooseButton = await driver.findElement(By.xpath('//*[@id="record_1"]'));
await wifiChooseButton.click();
}
// Check WiFi SSID Name
const ssidName = await driver.findElement(By.xpath('//*[@id="wlSsid"]'));
const ssid = await ssidName.getAttribute('value');
// Uncheck the hide password checkbox
const passCheckbox = await driver.findElement(By.xpath('//*[@id="hidewlWpaPsk"]'));
if (await passCheckbox.isSelected()) {
await passCheckbox.click();
}
// Clear the existing password and enter a new one
const passText = await driver.findElement(By.xpath('//*[@id="twlWpaPsk"]'));
const prevPass = await passText.getAttribute('value');
await passText.clear();
await passText.sendKeys(new_pass);
const newPass = await passText.getAttribute('value');
await driver.sleep(2000); // Wait for the page to load
// Submit the changes
const submitButton = await driver.findElement(By.xpath('//*[@id="btnApplySubmit"]'));
await submitButton.click();
await driver.sleep(3000); // Wait for the page to load
// Switch back to the default content
await driver.switchTo().defaultContent();
await driver.sleep(3000); // Wait for the page to load
return { ssid, prevPass, newPass };
}
To run the program, we can use the following command in the terminal:
node changeWifiPass.js --ip <router's ip, eg: "192.168.1.1"> -u <username login> -p <password login> --new-pass <new SSID pass>
The output of the program after running will look something like this:
Changing pass for SSID: INI BUKAN WIFI
Current Password: iniwifideng
New Password: selamattaunbaru
Changing pass for SSID: INI BUKAN WIFI 5G
Current Password: iniwifideng
New Password: selamattaunbaru
What if one (or many) frequencies have more than one SSID?
In this article, we can check the XPATH of the target SSID name. The example below selects one SSID whose password we want to change using XPATH (e.g., //*[@id="record_1"]).
# Make selection for SSID (if more than one SSID)
if (freq === '2g') {
// Select Secondary 2.4 GHz Tab
const wifiChooseButton = await driver.findElement(By.xpath('//*[@id="record_1"]'));
await wifiChooseButton.click();
}
Note:
The procedure above has a drawback where we have to check the HTML elements of the target SSID. If you want to select an SSID by its name, it would be better to check each SSID name automatically using a looping technique.
Finally, the complete code for this article can be accessed via github: https://github.com/irfanimaduddin/wifi-pass-change-js
Same procedure but in python: https://github.com/irfanimaduddin/wifi-pass-change-python


Let’s discuss