i need to send some key inputs to different browser sessions and want to make sure the right session is active.
i used this method, which is actually working.
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("User32.dll")]
private static extern IntPtr SetForegroundWindow(IntPtr hWnd);
driver = new ChromeDriver()
IntPtr browserWriteTarget1 = GetForegroundWindow();
driver = new ChromeDriver()
IntPtr browserWriteTarget2 = GetForegroundWindow();
driver = new ChromeDriver()
IntPtr browserWriteTarget3 = GetForegroundWindow();
SetForegroundWindow(browserWriteTarget1);
if (browserWriteTarget1 == GetForegroundWindow()
{
SendKeys.SendWait($"{myKey}");
Thread.Sleep(tWait);
}
The problem is if the user clicks around, while driver = new ChromeDriver is loading, that IntPtr browserWriteTarget = GetForegroundWindow(); will get the wrong value and the process will break.
The browser titles are all the same as im opening the same URL several times.
Is there a better solution, to avoid this?