9 Replies - 5520 Views - Last Post: 06 December 2009 - 09:07 AM Rate Topic: -----

#1 buynsell  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 14-October 09

How to click a button in webbrowser which......

Posted 01 December 2009 - 07:57 AM

Hi,

I am making a simple application which can login my internet page which i have to do manually whenever i open my computer, so i am noob in programming, i made a application which can fill me username and password form but there is no id or name for submit button, so is there any possibility to click with x,y axis or anything else.

here is the code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Wateen_Auto_Login
{
	public partial class Main : Form
	{
		public Main()
		{
	  
			
			InitializeComponent();
		}

		private void button1_Click(object sender, EventArgs e)
		{
			timer1.Stop();
			webBrowser1.Navigate("http://internet.wateen.net/home?t=g150nzv0");

		}

		private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
		{
			webBrowser1.document.GetElementById("Username").SetAttribute("value", txtname.Text);
			webBrowser1.document.GetElementById("Password").SetAttribute("value", txtpass.Text);
		 
		}

		private void txtname_TextChanged(object sender, EventArgs e)
		{
			
			
			
		}
	}
}




Is This A Good Question/Topic? 0
  • +

Replies To: How to click a button in webbrowser which......

#2 karimi  Icon User is offline

  • Banned
  • member icon

Reputation: -9
  • View blog
  • Posts: 104
  • Joined: 29-May 09

Re: How to click a button in webbrowser which......

Posted 01 December 2009 - 08:41 AM

Extemely poor question
Was This Post Helpful? 0
  • +
  • -

#3 buynsell  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 14-October 09

Re: How to click a button in webbrowser which......

Posted 01 December 2009 - 11:15 AM

View Postkarimi, on 1 Dec, 2009 - 07:41 AM, said:

Extemely poor question



what do you mean by poor question ???
If you know the answer then tell otherwise go ahead dont spam here.
Was This Post Helpful? 0
  • +
  • -

#4 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: How to click a button in webbrowser which......

Posted 01 December 2009 - 12:17 PM

are you just trying to manually log into a website with code? or do you want to actually use the website after you have logged in?
Was This Post Helpful? 0
  • +
  • -

#5 buynsell  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 14-October 09

Re: How to click a button in webbrowser which......

Posted 01 December 2009 - 12:26 PM

View Posteclipsed4utoo, on 1 Dec, 2009 - 11:17 AM, said:

are you just trying to manually log into a website with code? or do you want to actually use the website after you have logged in?



no, i dont want to use website once i press submit button my net starts working even i close that page, so i will put it in my startup thats it.
I only need to know how can i click on submit button which dont have any name or id, on my other application i used getelementbyid method which works perfect but how can i use it here when the button dont have any id or name, i think there is some function which can click on x,y axis.

Thanks
Was This Post Helpful? 0
  • +
  • -

#6 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: How to click a button in webbrowser which......

Posted 01 December 2009 - 01:07 PM

View Postbuynsell, on 1 Dec, 2009 - 03:26 PM, said:

View Posteclipsed4utoo, on 1 Dec, 2009 - 11:17 AM, said:

are you just trying to manually log into a website with code? or do you want to actually use the website after you have logged in?



no, i dont want to use website once i press submit button my net starts working even i close that page, so i will put it in my startup thats it.
I only need to know how can i click on submit button which dont have any name or id, on my other application i used getelementbyid method which works perfect but how can i use it here when the button dont have any id or name, i think there is some function which can click on x,y axis.

Thanks


No, there is no way to click on a button using x/y axis points. But don't fear, here is the way to do it...and you don't even need to use the .Net WebBrowser control.

First, download Tamper Data. It's an plugin for Firefox that allows you to tamper with POST data. We aren't going to actually tamper with the data, we just want to see what POST parameters the server is expecting.
https://addons.mozil...refox/addon/966

After installing it to Firefox, go to Tools --> Tamper Data. Open the plugin. Now go to the website that you need to go to, put in your username/password/whatever. At the top of the plugin, you will see "Start Tamper". Click it. then on the webpage, click the submit button.

Tamper Data will ask you if you want to "Tamper", "Submit", or "Abort Request". You will want to Tamper. Now you will see a window with Request data on the left side, and POST data on the right side. The POST data is what you are interested in.

The POST data will show you the data that is required to do a POST back to the server. You will need to know the parameter names.

For example, for OAuth authorization with Twitter, their POST parameters for the Deny/Allow screen are:

authenticity_token
oauth_token
session[username_or_email]
session[password]

Attached Image


Now that you have those, you can manually do a GET and POST request to the page to manually login.

// variables to store parameter values
string url = "http://SomeUrlToSomePage";
string username = "someUserName";
string password = "somePassword";
string authenticityToken = "moreText";
string oauthToken = "evenMoreText";

// creates the post data for the POST request
string postData = string.Format("session[username_or_email]={0}&session[password]={1}&authenticity_token={2}&oauth_token{3}", username, password, authenticityToken, oauthToken);

// create the POST request
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.ContentLength = postData.Length;

// POST the data
using (StreamWriter requestWriter2 = new StreamWriter(webRequest.GetRequestStream()))
{
     requestWriter2.Write(postData);
}

//  This actually does the request and gets the response back
HttpWebResponse resp = (HttpWebResponse)webRequest.GetResponse();

string responseData = string.Empty;

using (StreamReader responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream()))
{
     // dumps the HTML from the response into a string variable
     responseData = responseReader.ReadToEnd();
}

//  Now, find the index of some word on the page that would be 
//     displayed if the login was successful
int index = responseData.IndexOf("Hello");

if (index > -1)
    MessageBox.Show("we are successful");



That should work for you. You will need to replace the parameters with the parameters that are needed for your website. The parameters do not need to be in any particular order. Just make sure you have them all, and that they are concatenated with an ampersand ( & ) between them.
Was This Post Helpful? 1
  • +
  • -

#7 buynsell  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 14-October 09

Re: How to click a button in webbrowser which......

Posted 02 December 2009 - 08:44 AM

thanks for helping, need to know a little bit more, i never did anything with http web request, so i only have to add 2 text boxes and one button to login and which code is to be posted in login button ?
Was This Post Helpful? 0
  • +
  • -

#8 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: How to click a button in webbrowser which......

Posted 02 December 2009 - 08:52 AM

View Postbuynsell, on 2 Dec, 2009 - 11:44 AM, said:

thanks for helping, need to know a little bit more, i never did anything with http web request, so i only have to add 2 text boxes and one button to login and which code is to be posted in login button ?


you would put all of the code in the login button.
Was This Post Helpful? 0
  • +
  • -

#9 buynsell  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 14-October 09

Re: How to click a button in webbrowser which......

Posted 06 December 2009 - 08:45 AM

thanks got that, but still i am in trouble, i am conofigured everything as you said but still it not getting logged in, well here is my new code with tamper screenshot,


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;

namespace Wateen_Auto_Login
{
	public partial class Main : Form
	{
		public Main()
		{
			InitializeComponent();
		}

		private void Main_Load(object sender, EventArgs e)
		{
			using (StreamReader reader = File.OpenText("C:/login.txt"))
			{
				txtname.Text = reader.ReadLine();
				txtpass.Text = reader.ReadLine();
			}

			

		 }

		private void login_Click(object sender, EventArgs e)
		{
			// variables to store parameter values
			string url = "http://internet.wateen.net/home?t=g2vyriuj";
			string username = txtname.Text;
			string password = txtpass.Text;

			// creates the post data for the POST request
			string postData = string.Format("session[username]={0}&session[password]={1}", username, password);

			// create the POST request			
			HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
			webRequest.Method = "POST";
			webRequest.ContentType = "application/x-www-form-urlencoded";
			webRequest.ContentLength = postData.Length;


			// POST the data
			using (StreamWriter requestWriter2 = new StreamWriter(webRequest.GetRequestStream()))
			{
				requestWriter2.Write(postData);
			}

			//  This actually does the request and gets the response back
			HttpWebResponse resp = (HttpWebResponse)webRequest.GetResponse();
			string responseData = string.Empty;
			using (StreamReader responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream()))
			{
				// dumps the HTML from the response into a string variable
				responseData = responseReader.ReadToEnd();
			}
			
			//	 displayed if the login was successful

			int index = responseData.IndexOf("Hello");
			if (index > -1)

				label1.Visible = true;
			else MessageBox.Show("Failed");
		}	  

	   
	}
}



Posted Image
Was This Post Helpful? 0
  • +
  • -

#10 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: How to click a button in webbrowser which......

Posted 06 December 2009 - 09:07 AM

change this line...

string postData = string.Format("session[username]={0}&session[password]={1}", username, password);



to this...

string postData = string.Format("username={0}&password={1}", username, password);



notice that the two values are exactly the same as what Tamper Data shows.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1