I’ve WiMax that acting weird, sometimes when Windows is reporting I’m unable to connect to the Internet, the WiMax modem is showing strong signal strength, even the status page of the modem is showing operational as well, when this happen, I reboot it, I don’t go to the modem to reboot, I login via browser and reboot it using the web interface.
Even though I saved my login name and password, but clicking login button and clicking reboot button, and then clicking “yes” from the popup is too many steps for me, so I figured, I could just learn how to write code to automate a web page.
Using C#, it came with handle WebBrowser control, which rendering the passed in URL parameter using IE (I think, since all the context menu is identical with my IE one) on our Windows form. I tried to do this without UI interference, but not yet manage to get WebBrowser object to work in console application yet, so I ended up with a simple Windows application.
Adding in the WebBrowser control to the form, adding a progress bar for fun, and few lines of codes. On the application load, we navigate the browser to the modem login page.
browser.Navigate(@"http://\*.\*.\*.\*/index.php");
Adding this code to the DocumentCompleted event of the WebBrowser:
if (browser.Document.Body.Children["top_div"] == null) //logged out
{
lblMessage.Text = "Logging in…";
browser.Document.Forms[0].Children[1].SetAttribute("value", "thisismyadminid");
browser.Document.Forms[0].Children[4].SetAttribute("value", "thisismyadminpassword");
}
else
{
lblMessage.Text = "Reboot";
browser.Document.InvokeScript("ajaxReboot");
Application.Exit();
}
The IF statement check for the certain HTML that I’m sure that is only appear in the page after logged in, to decide whether now we’re logged in or not. If we’re logged out, then we want to log in, using Form[0]
to get the first and the only form in the document (because it does not provide ID, so I do it this way), and get the 2nd and 5th child of it, which is username box and password box, since both textbox doesn’t provide ID, so we can get it using element count.
After get both box, using SetAttribute, to set the value to my login id and password, I’m suppose to trigger the login button click after that, but it apparently triggered before certain script loaded, so I put it on other event.
In the ProgressChanged event of the browser:
if (e.CurrentProgress == 0) {
browser.Document.Body.Children[0].Children[1].InvokeMember("click");
}
This one could be failed so bad if use this application against web page over the Internet, but it seems to works on my local network. Anyway, the code check for current progress, if is 0 (after I debug, found that it only show 0 when it loaded, when it start load, I already got 10), then we trigger the click of the login button.
After this, the browser control will loading the page again, so it will hit the else statement up there, then we call the javascript that the reboot button call when I manually go click on it.
Tada! Lazy man automation…