input data to website  
Author Message
ckob





PostPosted: Visual C# General, input data to website Top

Ok right now I have to configure routers manually by inserting the exact same data into a router's configuration panel (website on router) then swap out the configured router with one that needs the exact same setup and redo this process over and over. I would like to write an application that fills in all the needed boxes,selects values from drop down menu's and fills in any other information and presses the save & restart button for me is this possible I initially though about trying to find the file on the router that its writing to thinking making its one config file maybe if thats the case I could just write to the file but im not entirely sure.


Visual C#14  
 
 
ahmedilyas





PostPosted: Visual C# General, input data to website Top

you wont be able to write to the file. its highly protected and encrypted. You could use a webbrowser control, if the router is web configurable, then kind of automate the process on the documentcompleted event of the webbrowser control and set the inputs/dropdown boxes via the aid of GetElementById using the Document property of the control. Would this get you started

otherway is to manually do it in that, you can automatically launch an IE instance, navigate to the page you want, then control your mouse and keyboard and send keys to IE however this wouldnt be better, the first option would as everything you pretty much need is there and doesnt require you to not use your PC at the same time



 
 
ckob





PostPosted: Visual C# General, input data to website Top

right your first idea sounds like what I want to do, could you point me to some tutorials or even an example on how to do this im completely lost when it comes to this.

 
 
ahmedilyas





PostPosted: Visual C# General, input data to website Top

it's a tricky one really since you need to know what the page has in terms of elements and its names etc...

http://msdn2.microsoft.com/en-us/library/system.windows.forms.webbrowser.aspx

http://msdn2.microsoft.com/en-us/library/system.windows.forms.htmldocument.getelementbyid.aspx

http://msdn2.microsoft.com/en-us/library/system.windows.forms.htmldocument.getelementbyid(VS.80).aspx

 

once you get hold of an element, just set its innerText property then submit it or whatever so it can go ahead and process it.

Example, I wanted to look for an element called "txtSearch" in the current document.

 

HtmlElement theElement = this.theWebBrowserControl.Document.GetElementById("txtSearch");

if (theElement != null)

{

   theElement.InnerText = "hi!"; //sets the text inside this control

}



 
 
ckob





PostPosted: Visual C# General, input data to website Top

so if I was able to get the name of each element I could use this    Also can I edit text box text as well

 
 
ahmedilyas





PostPosted: Visual C# General, input data to website Top

yes! you can write/edit (same thing) once you got the element you are after

 
 
ckob





PostPosted: Visual C# General, input data to website Top

ok what would I do to find the text in this box.
<input name=hostName size=15 maxLength=39 value='banished' style=font-family: Arial; font-size: 10pt>(Required by some ISPs)</font></td></tr><tr><th align=right width=186 bgcolor=#000000><font face=arial color=#FFFFFF size=2>Domain Name :&nbsp;</font></th><td width=559>&nbsp;&nbsp;&nbsp;<font face=arial size=2>


the name is hostName so I tried but it didnt show anything.



private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
HtmlElement es = webBrowser1.Document.GetElementById("hostName");
// foreach (HtmlElement ele in es)
// {
if (es != null)// && es.Count != 0)
{
mystring = es.InnerText;
textBox1.Text = mystring;
// IsIPAddress(mystring.Trim());
}
else
{
errorProvider1.SetError(textBox1, "No Data");
}
// }
}

private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.Navigate("http://192.168.1.1");
}
}
}

 
 
ahmedilyas





PostPosted: Visual C# General, input data to website Top

hmm. I'm not sure what you are trying to do in regards to where you are going to be configuring it, meaning if you are going to be taking values out from the element into your own textbox or if you are going to be modifying the values in that same textbox on the webpage.

if its on the webpage then you almost have it....

HtmlElement es = webBrowser1.Document.GetElementById("hostName");

if (es != null)
{
es.InnerText = "new value";


}
else
{
errorProvider1.SetError(textBox1, "No Data");
}

but yes your generally along the right lines



 
 
ckob





PostPosted: Visual C# General, input data to website Top

First let me say I really appreciate all your help im beginning to understand how this works but I have a few more questions.

I can now edit the input boxes my question is:

once I have added my text to the input box how do I press a submit button and retrieve info from the next page that loads. To give you an idea

http://www.powerdog.com/wepkey.cgi

That website what I am doing is using this program to input a phrase into the input box and I would like it to press submit and then retrieve the first key from the list on the following page.

 
 
ahmedilyas





PostPosted: Visual C# General, input data to website Top

no worries. hehe, funny, I was just replying to someone with the same thing about invoking the button. I'm not sure how this will work with perl script, being the same or not but...lets give a try.

HtmlElementCollection theElements = this.webBrowser1.Document.GetElementsByTagName("input"); 'get an input element
foreach (HtmlElement curElement in theElements)
{
if (curElement.GetAttribute("Value").Equals("submit")) 'get the value of the current element. is it a submit type if so...
{
curElement.InvokeMember("click"); 'invoke the click event!
}
}

in regards to retrieving the next page, well, in the documentcompleted event, as you have, this will be fired when the document has finished loading. So to be safe, I would suggest:

  • checking the expected URL you are wanting to do your editing/parsing/operations on the page so you don't "break" the code in that, you are expecting one page but navigated to the next page where those elements you are trying to find, do not exist.

    Once checking the current page url, then execute the operations you want to execute on that method

    now, in regards to retrieving the data on the page, you need to either go through the elements as you currently are doing, or look for the innertext/outertext for the elements and see what the responses are/values are.

    innertext contains the text in that control, as well as the ability to set it



  •  
     
    ckob





    PostPosted: Visual C# General, input data to website Top

    ok This is what I have now.

    HtmlElement PhraseInput = webBrowser1.Document.GetElementById("phrase");
    HtmlElementCollection theElements = this.webBrowser1.Document.GetElementsByTagName("input");
    if (PhraseInput != null)
    {
    PhraseInput.InnerText = "test phrase";

    }
    foreach (HtmlElement curElement in theElements)
    {
    if (curElement.GetAttribute("Value").Equals("submit"))
    {
    curElement.InvokeMember("click");
    }

    }

    still inputs the phrase ok but it doesn't press the button and as far as retrieving the info on the next page..you lost me :P



     
     
    ahmedilyas





    PostPosted: Visual C# General, input data to website Top

    yeh its one of those hit and miss things. hmm. Can't suggest/think of anything for now. when you step through the de**** (place a breakpoint on the first line and step through it in code), does it go in the if statement do it go in the for each statements/loop and the inner if loop too

     
     
    ckob





    PostPosted: Visual C# General, input data to website Top

    how would I go about selected drop down menu items

     
     
    ahmedilyas





    PostPosted: Visual C# General, input data to website Top

    Good question. That is a bit more tricky and im not sure. I'll have to get back to you on that one. you can do it in the same way but then its about selecting the option which will be trickier.

     
     
    ckob





    PostPosted: Visual C# General, input data to website Top

    any luck