Click here to Skip to main content
15,357,293 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, It's Isabel García from Barcelona and I am finding some problems with PostMessage method to send a mouse event to a web browser.

C#
public void onClick2()
    {
        Process[] notepads = Process.GetProcessesByName("chrome");
        if (notepads.Length == 0) return;
        if (notepads[0] != null)
        {
            UnityEngine.Debug.Log("Encontrado");
            hWnd = FindWindowEx(notepads[0].MainWindowHandle, new IntPtr(0), "Edit", null);
         
            PostMessage(hWnd, MOUSEEVENTF_RIGHTDOWN, VK_RETURN, 0x001C0001);
            PostMessage(hWnd, MOUSEEVENTF_RIGHTDOWN, VK_DOWN, 0x00500001);

        }

    }


The button in which I have to click has a unique id:
<a href="#" id="action-button" title="Share on WhatsApp" class="button button--simple button--primary">Send</a>

And I would like to access to that id. Using Spy++ I am not able to access to that button.

What I have tried:

I have used SendKeys but it has worked with Notepad app.
I have used Spy++ but without success with chrome web page.
I have perfom a click doing the following:
C#
private const uint MOUSEEVENTF_MOVE = 0x0001;
    private const uint MOUSEEVENTF_LEFTDOWN = 0x0002;
    private const uint MOUSEEVENTF_LEFTUP = 0x0004;
    private const uint MOUSEEVENTF_RIGHTDOWN = 0x0008;
    private const uint MOUSEEVENTF_RIGHTUP = 0x0010;
    private const uint MOUSEEVENTF_ABSOLUTE = 0x8000;
public void performClick(uint x, uint y)
    {
        //SetCursorPos(x, y);
        mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTDOWN, x, y, 0, UIntPtr.Zero);
        Thread.Sleep(200);
        mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTUP, x, y, 0, UIntPtr.Zero);
        UnityEngine.Debug.Log("performClick x: " + x  + "y: "+ y);
        //PostMessage(handle, MOUSEEVENTF_LEFTDOWN, 5, 5); https://stackoverflow.com/questions/409886/send-key-strokes-to-games-using-direct-input/50855426#50855426
    }
Posted
Updated 13-May-19 1:44am
Comments
Gerry Schmitz 13-May-19 12:18pm
   
Are you using the "Browser control"? If not why not? If you do, you can access the DOM.
Richard Deeming 14-May-19 12:01pm
   
If you're trying to automate a web browser, why not use a tool that's specifically built to do that?
Selenium - Web Browser Automation[^]

Spy++ and PostMessage will only target controls which have their own window handle. Browsers don't create a separate window handle for every element on the page. If they did, the performance and memory usage would be horrendous.

1 solution

You probably need to make the window active first, see article here: Sending Keystrokes to another Application in C#[^]
   
Comments
Member 13615623 13-May-19 8:52am
   
Hello! Thank you very much for the quick response. I have tried the following:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

const int WM_KEYDOWN = 0x100;
const int WM_KEYUP = 0x101;
const int WM_SYSKEYDOWN = 0x104;
const int VK_BACK = 0x08;
const int VK_F5 = 116; //Ox74

[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

[DllImport("user32.dll")]
static extern byte VkKeyScan(char ch);

[DllImport("User32.Dll", EntryPoint = "PostMessageA")]
static extern bool PostMessage(IntPtr hWnd, uint msg, int wParam, int lParam);


private void Form1_Load(object sender, EventArgs e)
{

}

private void button1_Click_1(object sender, EventArgs e)
{
string msg = "123456";
IntPtr noteWnd = FindWindow("Notepad", null);
if (!noteWnd.Equals(IntPtr.Zero))
{
IntPtr editWnd = FindWindowEx(noteWnd, IntPtr.Zero, "Edit", null);
if (!editWnd.Equals(IntPtr.Zero))
{
for (int i = 0; i < msg.Length; i++)
{
PostMessage(editWnd, WM_KEYDOWN, VkKeyScan(msg[i]), 0);
}
PostMessage(editWnd, WM_KEYDOWN, VK_BACK, 0);
PostMessage(editWnd, WM_KEYDOWN, VK_BACK, 0);
//PostMessage(editWnd, WM_SYSKEYDOWN, VkKeyScan('F'), 1 << 29);
PostMessage(editWnd, WM_KEYDOWN, VK_F5, 0);
PostMessage(editWnd, WM_KEYUP, VK_F5, 0);
//PostMessage(editWnd, WM_KEYUP, VK_F, 0);
}
}
}
}
}
Member 13615623 13-May-19 9:09am
   
It does work with notepad without focusing on the screen. However, I can not make it works with browser!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900