Sign in to follow this  

[.net] [C#] Webbrowser + autoclick

This topic is 3100 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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");
}
}
}

Share this post


Link to post
Share on other sites
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. :|

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 3100 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this