Karthi Keyan

Karthi Keyan

  • 1.3k
  • 724
  • 125.4k

Create web automation using C# windows application.

May 17 2017 7:43 AM
Hi,
   I need to create the Windows application. That login into the website with username and password, then click one hyperlink label. And finally download the files from the website. now i am in first level. i tried some codes to access the website using webBrowser. but that's not work. 
   The login button in website not having the id. i don't know, am i going in wrong or right way.    The below code doesn't give any output. simply run and got out from the switch funtion. can any one help me.
 
Thankz in advance. 
 
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Pronoun_Automation
{
public partial class Form1 : Form
{
private int page;
public Form1()
{
InitializeComponent();
}
private void makeBrowser()
{
webBrowser1 = new WebBrowser();
webBrowser1.Name = "webBrowser1";
}
private void showBrowserInForm()
{
webBrowser1.Location = new Point(12, 300);
webBrowser1.MinimumSize = new Size(20, 20);
webBrowser1.Size = new Size(783, 338);
webBrowser1.TabIndex = 1;
Controls.Add(webBrowser1);
}
private void button1_Click(object sender,EventArgs e)
{
page = 1;
makeBrowser();
showBrowserInForm();
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(DocumentCompleted);
webBrowser1.Navigate("https://xxx.com/admin/login/");
}
private void login()
{
HtmlElement name = webBrowser1.Document.GetElementById("UserName");
if (name != null)
{
name.InnerText = "username";
}
HtmlElement password = webBrowser1.Document.GetElementById("password");
if (password != null)
{
password.InnerText = "password";
}
HtmlElement submit = webBrowser1.Document.GetElementById("submit");
if (submit != null)
{
submit.InvokeMember("click");
}
}
private void gotoData()
{
webBrowser1.Navigate("https://xxx.com/admin/login/");
}
private void getData()
{
string text = webBrowser1.DocumentText;
Regex ex1 = new Regex(@"used\s+(\d+.\d+)");
string used = ex1.Match(text).Groups[1].ToString();
textBox1.Text = used;
webBrowser1.Dispose();
}
void DocumentCompleted(object sender,WebBrowserDocumentCompletedEventArgs e)
{
if (webBrowser1.ReadyState != WebBrowserReadyState.Complete) return;
switch (page)
{
case 1:
page = 2;
login();
break;
case 2:
page = 3;
gotoData();
break;
case 3:
page = 4;
getData();
break;
}
}
}
}
 

Brought to you by:

Answers (3)