How to let the application automatically press enter after opening a website

Viewed 29

Hi I am using C# to open a website and I would like to let the application press enter after opening the website. Is anyone know how to solve this problem?

            System.Diagnostics.Process.Start(new ProcessStartInfo
            {
                FileName = OpenWebsite,
                UseShellExecute = true
            });
           
            System.Threading.Thread.Sleep(5000);
1 Answers

I think of two ways to do this.

  • Send the key to the process

Like that

// You need this
[DllImport ("User32.dll")] static extern int SetForegroundWindow(IntPtr point);

// ...

// The target process (here with firefox) but be sure to target the good one
Process p = Process.GetProcessesByName("firefox").FirstOrDefault();
if (p != null)
{
    IntPtr h = p.MainWindowHandle;
    SetForegroundWindow(h);
    SendKeys.SendWait("Enter");
}
  • Open whe website in your application and simulate a clic

With a webview2

// ...

// wbv2 is a WebView2 component displayed in your UI  
wbv2.Source = new Uri([your URL], UriKind.Absolute);

// When the navigation is complete I click in the first button
await wbv2.ExecuteScriptAsync(document.getElementsByName('button')[0].click()");

This is not the full code but I guess you have some clues for your goal.

Related