How to select dynamic radio option

I want to select a radio option it is basically according to the specific WiFi MAC address such as the picture below, let's say I want to select the router with 18:90:d8:61:5d:68 MAC address every time this window pops up.

Now the challenge is that each time this window opens up the list above will sort according to signal strength so the list orders will change and the radio selection tags will change every time.

Is it possible to make the script to select the radio button according to the Wifi MAC's address ?

P.S. Every time the order list changes the id="2" will change its value.

Yes, if the MAC address is shown in the above table.
I didn’t see it, so I used the Network Name.

Put this JavaScript in a Execute a JavaScript in Front Browser action (KM Wiki)

JavaScript to Select Radio Button

//--- Find Table Cell (<td>) That Has Unique Text to identify the row of the Radio Button ---
var tdText = 'PTCL-BB';

var tdElems = document.evaluate("//td[contains(., tdText)]", document, null, XPathResult.ANY_TYPE, null );
var tdElem = tdElems.iterateNext();

//--- Now Get the Parent Element (the Table Row (<tr>) ---
var parElem = tdElem.parentNode

//--- Get the Radio Button (<input>) in the First Cell of this Table Row
var btnRadio = parElem.querySelector('td input')

//--- Select (click) the Radio Button ---

I used this test model of your web page:

<tr id="row_2">

<td>ll (B+G-+N)</td>
  <input type="radio" name="ap_enable">




Awesome! Thank you so much for taking the time to make this JS script. BTW the MAC address is 18:90:d8:61:5d:68 so can I use:

var tdText = '18:90:d8:61:5d:68';

to find the radio button?

P.S. The full code on the webpage looks like this:

That should work. Just try it, and let us know.

I would like to really love however I have been stuck with a link that I can not click even with Xpath. Could you please have a look at this post:

I’m not sure I understand your reply. Did my above solution work for you?

Alright I have got the AP Scan button working but the above solution didn't worked for selecting the radio button. Here is the macro if that could help:

connect to the router.kmmacros (6.0 KB)

I can not post the whole code here because of the forum limitations so I upload it on pastebin:

OK, now that I have the complete HTML, I think I’ve found a solution to select (click) the desired radio button:

JavaScript in Browser to Click Button

//--- Get List of All Cells in Network Table --
// Network Table has class="df_tab"

var elemList = document.querySelectorAll('table.df_tab td');
var searchText = "18:90:d8:61:5d:68"; // text in Cell to search for
var elemFound;

//--- Search for <td> That Has text ---
for (var i = 0; i < elemList.length; i++) {
  if (elemList[i].textContent === searchText) {
    elemFound = elemList[i];

if (elemFound) {

  //--- Now Get the Parent Element (the Table Row (<tr>) ---
  var parElem = elemFound.parentNode;

  //--- Get the Radio Button (<input>) in a cell in this Table Row
  var btnRadio = parElem.querySelector('td input');

  //--- Select (click) the Radio Button ---;

} else { alert('Could NOT Find Table Cell with text: ' + searchText)}

Also, in your Macro, you need to use EITHER Safari OR Chrome, but NOT both. Looks like you need to delete the Safari Actions.

Ref HTML Used to Develop Script

<table class="df_tab"  cellspacing="1" cellpadding="3" style="width: 800px;">
    <thead class="df_thead">
            <td rowspan="1">ID</td>
            <td rowspan="1">Network Name</td>
            <td rowspan="1">BSSID</td>
            <td rowspan="1">Channel(Radio Band)</td>
            <td rowspan="1">Radio Mode</td>
            <td rowspan="1">Authentication Type</td>
            <td rowspan="1">Signal Strength</td>
            <td rowspan="1">Connect</td>
    <tbody class="df_tbody">
        <tr id="row_0">
            <td>7 (B+G+N)</td>
            <td><input type="radio" name="ap_enable" value="Connect" id="0" onclick="get_ap_info("></td>
        <tr id="row_1">
            <td>Gigalink (C) 03004909212</td>
            <td>3 (B+G+N)</td>
            <td><input type="radio" name="ap_enable" value="Connect" id="1" onclick="get_ap_info("></td>
        <tr id="row_2">
            <td>ZONG MBB-E8372-4CD9</td>
            <td>7 (B+G+N)</td>
            <td><input type="radio" name="ap_enable" value="Connect" id="2" onclick="get_ap_info("></td>
        <tr id="row_3">
            <td>1 (B+G+N)</td>
            <td><input type="radio" name="ap_enable" value="Connect" id="3" onclick="get_ap_info("></td>

Dang! I just can’t get around with the idea this level of support is free. Seriously how long and from where have you been learning this JS language cause I was also learning and I know that its not an easy nut to crack. And why you helped so much do you get paid for it? I am sorry while I really appreciate your help I am also very curious.

P.S. Also curious to know that how long did it took you to come up with this above script?

1 Like

There are a number of others who also provide help for free in this forum. Most of us do it because we enjoy helping others, and we almost always learn something when solving a problem. It also helps keep me sharp/current in the various technologies.

Well, I have a long history in software development, with many different languages. I initially learned JavaScript back in the late 90s doing web design/development, but then left it for years until Apple introduced JavaScript for Automation (JXA) to the Mac scripting environment. Over the last year or so I have become very interested in web page scraping, mostly for my own benefit to pull data/information of interest to me.

While I have read (partially) several JavaScript books, and have taken a couple of online courses, I have learned the most on my own, just by trial-and-error, and Google searching. A lot of what I have learned comes from the forums.

The best trick I have learned is to open a web page in Chrome, do an “inspect” on the text of interest to open the Chrome Dev Tools IDE. This allows me to quick view/inspect the HTML code, and then to quickly test various statements in the Chrome JavaScript Console.

Once I had a good representation of the HTML code, it only took me about 20-30 minutes, maybe less. When doing stuff for myself where I have the actual page URL, I can often develop the JavaScript I need within 5-10 minutes. But that is after many hours in the past of studying/testing some of the key JavaScript methods, like querySelector and use of XPaths.


The level of support provided on this forum is phenomenal, it far exceeds my wildest dreams for the forum.

To be clear, no one (other than me) is paid anything to answer questions here. The thousands of answers than Jim and others have provided here are done entirely voluntarily - I know this has surprised more than a few people. There is no way I could provide a fraction of the assistance that is provided here and still get any time to develop Keyboard Maestro, and frankly, there are many people here who have way more experience in many topics than I do anyway, so I don’t even have the skills to answer many of the questions.

We are all very blessed to have such a large and helpful and committed community on this site, and I really do think it has to be one of the most helpful forums on the Internet - certainly I can’t think of any other forum that comes close.