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 ---
btnRadio.click()

I used this test model of your web page:

<html>
<head></head>
<body>
<table>
<tbody>
<tr id="row_2">

<td>2</td>
<td>PTCL-BB</td>
<td>18:90:d8:61:5d:68</td>
<td>ll (B+G-+N)</td>
<td>AP</td>
<td>WPA2-PSK</td>
<td>44%</td>
<td>
  <input type="radio" name="ap_enable">
</td>

</tr>
</tbody></table>



</body></html>

Questions?

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:

https://pastebin.com/embed_js/SfYkHuMY

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:

https://pastebin.com/8SWMkpZL

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];
    break;
  }
}

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 ---
  btnRadio.click();

} 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

<html>
<body>
<table class="df_tab"  cellspacing="1" cellpadding="3" style="width: 800px;">
    <thead class="df_thead">
        <tr>
            <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>
        </tr>
    </thead>
    <tbody class="df_tbody">
        <tr id="row_0">
            <td>0</td>
            <td>PTCL-BB</td>
            <td>18:90:d8:61:5d:68</td>
            <td>7 (B+G+N)</td>
            <td>AP</td>
            <td>WPA2-PSK</td>
            <td>40%</td>
            <td><input type="radio" name="ap_enable" value="Connect" id="0" onclick="get_ap_info(this.id)"></td>
        </tr>
        <tr id="row_1">
            <td>1</td>
            <td>Gigalink (C) 03004909212</td>
            <td>68:72:51:3c:31:dd</td>
            <td>3 (B+G+N)</td>
            <td>AP</td>
            <td>no</td>
            <td>32%</td>
            <td><input type="radio" name="ap_enable" value="Connect" id="1" onclick="get_ap_info(this.id)"></td>
        </tr>
        <tr id="row_2">
            <td>2</td>
            <td>ZONG MBB-E8372-4CD9</td>
            <td>84:9f:b5:85:4c:d9</td>
            <td>7 (B+G+N)</td>
            <td>AP</td>
            <td>WPA2-PSK</td>
            <td>24%</td>
            <td><input type="radio" name="ap_enable" value="Connect" id="2" onclick="get_ap_info(this.id)"></td>
        </tr>
        <tr id="row_3">
            <td>3</td>
            <td>PTCL-BB</td>
            <td>e8:cc:18:69:62:cd</td>
            <td>1 (B+G+N)</td>
            <td>AP</td>
            <td>WPA-PSK/WPA2-PSK</td>
            <td>8%</td>
            <td><input type="radio" name="ap_enable" value="Connect" id="3" onclick="get_ap_info(this.id)"></td>
        </tr>
    </tbody>
</table>
</body>
</html>

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 stackoverflow.com 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.

3 Likes

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.

5 Likes