[GeckoFx C#] How to click html elements and input text

Requirements:
- Basic coding knowledge
- Visual Studio (Im using Visual Studio 2013 you can download it from official website. Dont install cracked ones etc, free one works well)


Ill show you here how I do different tasks in geckofx embedded browser. Have in mind that I will update this thread with more things like how to remove ads so we wont need to load them, how to wait for element to appear and so on..


How to click html elements - button, image, checkbox, radiobox or any other clickable html object which we need to click.

ClickByText
      public async Task ClickByText(GeckoWebBrowser wBrowser, string TagName, string TextContent, int TimeOut = 15)
{
bool Found = false;
int Failed = 0;
for (int i = 1; i <= TimeOut; i++) //We will try to search element for 15 seconds
{
try
{
foreach (GeckoHtmlElement geckoHtmlElement in wBrowser.Document.GetElementsByTagName(TagName))
{
if (!geckoHtmlElement.TextContent.Contains(TextContent)) { Failed = Failed + 1; }
if (Failed >= 1) { Failed = 0; continue; }
else if (Failed < 1) { geckoHtmlElement.Click(); Found = true; break; }
}
if (Found == true) { return true; }
await Task.Delay(1000);
}
catch (Exception e) { MessageBox.Show("Error clicking element 1 " + e); return false; }
}
return false;
}


With this method we can find html element by its tag and text content. For example when inspecting like button in facebook we get something like this
User posted image
I believe many of you who tried iMacros ran into trouble when tried to click this html element and its because id and other attributes chages all the time. You would need some parsing if you want to get right attribute and click it.
We dont need attribute because that button have its text content which is 'Like'. So we just use this function :
await ClickByText(wBrowser, "button", "Like");


ClickByAttribute
What if there is no text content or it always changes? We can easly locate element by its attribute.
public async Task ClickByAttribute(GeckoWebBrowser wBrowser, string TagName, string attributeType, string attributeText, int TimeOut = 15)
{
bool Found = false;
int Failed = 0;
for (int i = 1; i <= TimeOut; i++)
{
try
{
foreach (GeckoHtmlElement geckoHtmlElement in wBrowser.Document.GetElementsByTagName(TagName))
{
if (!(geckoHtmlElement.GetAttribute(attributeType) == attributeText)) { Failed = Failed + 1; }
if (Failed >= 1) { Failed = 0; continue; }
else if (Failed < 1) { geckoHtmlElement.Click(); Found = true; break; }
}
if (Found == true) { return true; }
await Task.Delay(1000);
}
catch (Exception e) { MessageBox.Show("Error clicking element 2" + e); return false; }
}
return false;
}


Lets click 'Rules' link in our a2s forum. When we inspect element we get this User posted image
So our TagName is "a" and we have attributeType which is "href" and its text - "//a2s.in/announcements/4-rules-regulations.html".
await ClickByAttribute(wBrowser, "a", "href", "//a2s.in/announcements/4-rules-regulations.html");



How to Input Text

  public async Task InputByAttribute(GeckoWebBrowser wBrowser, string TagName, string AttributeType, string AttributeText, string YourText,int TimeOut = 15)
{
bool Found = false;
int Failed = 0;
for (int i = 1; i <= TimeOut; i++)
{
try
{
foreach (GeckoInputElement geckoHtmlElement in wBrowser.Document.GetElementsByTagName(TagName))
{
if (!(geckoHtmlElement.GetAttribute(AttributeType) == AttributeText)) { Failed = Failed + 1; }
if (Failed >= 1) { Failed = 0; continue; }
else if (Failed < 1) { geckoHtmlElement.Value = YourText; Found = true; break; }
}
if (Found == true) { return true; }
await Task.Delay(1000);
}
catch (Exception e) { MessageBox.Show("Error filling info 1" + e); return false; }
}
return false;
}


With this method we can locate inputbox and fill in information. For example lets input message in our subbox input box User posted image
When we inspect element we get this User posted image so to be able to fill text into this inputbox we need TagName, AttributeText and ofcourse your text which you want to input

await InputByAttribute(wBrowser,"input", "class", "primary full textbox", "Hello");




Example
Since we have methods we can use them in automating something. Lets try to login into our forum. We will use both click element and input methods.

 public async Task Login(GeckoWebBrowser wBrowser, string username, string password)
{
//Fill username
await InputByAttribute(wBrowser, "input", "id", "navbar_username", username);
//Fill password
await InputByAttribute(wBrowser, "input", "id", "navbar_password_hint", password);
//Click button to login
await ClickByAttribute(wBrowser, "input","type","submit");
}



Here is base project where you can try to code. I already included geckofx browser, shown methods and login into warofclicks example. You can try to continue automating it.. https://www.dropbox.com/s/bbvikdim7e4w3hh/MyFirstBot.rar?dl=1

---------------------------------------------------------
If you never coded before then you cant just jump in into more advanced things. You have to read basic things about C# and know how everything works. You dont need to memorize it just know where needed info is located.
If you get too many errors then dont worry about it, fix them one by one.

---------------------------------------------------------

To Do:
- List with compilation of most useful ebooks to learn C#
- Tutorial how to include newest geckofx files into your C# project
- Geckofx bot examples
- Http request tutorial
- How to obfuscate your code and decompile other .net applications
- Much more


---------------------------------------------------------
Side note: Please ask all questions and even stupid ones about this subject. I'm bad teacher and cant know what you dont understand so Ill try my best to explain everything if you ask.

Important: Dont post offtopic messages here. Only write/ask about coding.
Thanks for sharing [MENTION=3601]EdgarT[/MENTION] , I'm downloading Visual Basic right now , never used before , but this really helpful , I guess using this I can automate filling promo pages !? also can you add in your next updates how to include filling captcha , since some websites use captcha!?
[MENTION=1]Angemon[/MENTION], it wont work if you just download visual studio for first time and try to code. You have to read basic things first and study them for a while.
Filling captcha? "How to Input Text" shows you how to fill input box so I should only show you how to grab captcha pictures and put them into form.

Promo pages are easy, if you could give me link of promo page then I could make example.
EdgarT;25017 wrote:[MENTION=1]Angemon[/MENTION], it wont work if you just download visual studio for first time and try to code. You have to read basic things first and study them for a while.
Filling captcha? "How to Input Text" shows you how to fill input box so I should only show you how to grab captcha pictures and put them into form.

Promo pages are easy, if you could give me link of promo page then I could make example.

I know the code won't work just like that , we have already learned C++ in school so I guess with a little of work I will be able to have a use for your code.