[.net] [C#] Webbrowser + autoclick

Started by
4 comments, last by capn_midnight 14 years, 10 months ago
Hi, I'm trying to make a program to make a program that makes you able to surf to a page. Then you should be able select a few things (selecting = just one mouseclick) (so you'd be able to give in a set of coordinates and it would click on those places in the browser) And for the coordinates, you should be able to hover over the starting position, then press a so it knows that the first click will be there, from then on I could just adjust the other coordinates according tot that position : ) (but there is no mousemove event for the webbrowser.. ? ) BUT the problem is.. None of the things I've tried seem to work :S - to start of with, I've really searched a lot to get it to click inside the webbrowser but it doesn't seem to work.. here's the code I have so far..
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.Runtime.InteropServices;

namespace WindowsFormsApplication1
{
   

	public partial class Form1 : Form
	{
		[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
		static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

		[DllImport("user32.dll", SetLastError = true)]
		static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd);

		[DllImport("user32.dll", CharSet = CharSet.Auto)]
		static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
		int X = 5;

		public Form1()
		{

			InitializeComponent();
		}

		private void button1_Click(object sender, EventArgs e)
		{
			site.Navigate(input.Text);


		}

		private void button2_Click(object sender, EventArgs e)
		{
			IntPtr handle = site.Handle;
			StringBuilder className = new StringBuilder(100);
			while (className.ToString() != "Internet Explorer_Server") // your mileage may vary with this classname
			{
				handle = GetWindow(handle, 5); // 5 == child
				GetClassName(handle, className, className.Capacity);
			}
			IntPtr lParam = (IntPtr)((y << 16) | x); // X and Y coordinates of the click
			const IntPtr wParam = IntPtr.Zero; // change this if you want to simulate Ctrl-Click and such
			const uint downCode = 0x201; // these codes are for single left clicks
			const uint upCode = 0x202;
			SendMessage(handle, downCode, wParam, lParam); // mousedown
			SendMessage(handle, upCode, wParam, lParam); // mouseup
				
		}

		private void button2_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar == (char)Keys.A)
			{
				MessageBox.Show("blub");
			}
		}
	}
}
Error 1 The name 'y' does not exist in the current context C:\Users\Pieter\AppData\Local\Temporary Projects\WindowsFormsApplication1\Form1.cs 49 39 WindowsFormsApplication1 Error 2 The name 'x' does not exist in the current context C:\Users\Pieter\AppData\Local\Temporary Projects\WindowsFormsApplication1\Form1.cs 49 50 WindowsFormsApplication1 Error 3 The type 'System.IntPtr' cannot be declared const C:\Users\Pieter\AppData\Local\Temporary Projects\WindowsFormsApplication1\Form1.cs 50 19 WindowsFormsApplication1
Advertisement
I'm trying a different aproach, but that's not working either..
(I simplified things just to test code..)


if I press button 1, button 2 should be pressed as wel..

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.Runtime.InteropServices;namespace WindowsFormsApplication1{	public partial class Form1 : Form	{		[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]		public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);		private const int MOUSEEVENTF_LEFTDOWN = 0x02;		private const int MOUSEEVENTF_LEFTUP = 0x04;		private const int MOUSEEVENTF_RIGHTDOWN = 0x08;		private const int MOUSEEVENTF_RIGHTUP = 0x10;		public void DoMouseClick()		{			//Call the imported function with the cursor's current position			int X = Cursor.Position.X;			int Y = Cursor.Position.Y;			mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 410, 180, 0, 0);		}		public Form1()		{			InitializeComponent();		}		private void Form1_Load(object sender, EventArgs e)		{		}		private void button1_Click(object sender, EventArgs e)		{			DoMouseClick();		}		private void button2_Click(object sender, EventArgs e)		{			MessageBox.Show("blub");		}	}}
I'm not quite sure what it is you're going for. :S

However, a quick google for "webbrowser mouse events" has this as the first result. Seems there is a webBrowser.Document.Click event, if that is sufficient for what you want. :|
Quote:Original post by nerd_boy
I'm not quite sure what it is you're going for. :S

However, a quick google for "webbrowser mouse events" has this as the first result. Seems there is a webBrowser.Document.Click event, if that is sufficient for what you want. :|


The idea is that:

I have a webbrowser in the application.
I can hold my mouse over a location in the webbrowser and press a --> coordinates are stored.
Then I would be able to press a button in the program (at the top) that initiates a sequence of clicks, based on the starting coordinates

so starting coordinates were (a,b)

What the program does:

click (a-200,b+50)
click (a-200,b+30)
etc.

I hope that's a bit more clear?
Are you trying to automate a web browser?
That is very tricky as the locations of hyperlinks and buttons are very hard to pinpoint. You also need exact knowledge of how and what the browser renders the html.

Have a look at this to see a kind of unit testing of web pages using recording and playback. Similar robot/automation tools exist, just search the web. I hope this helps.
you would probably have an easier time writing &#106avascript that processed the DOM.

[Formerly "capn_midnight". See some of my projects. Find me on twitter tumblr G+ Github.]

This topic is closed to new replies.

Advertisement