I can't open browser with WatiN in C#

58 Views Asked by At

I'm programming a basic script using C#. I want to make an script what open Internet Explorer (or any other browser) and clicking, but, always have the same error, even when use FireFox: It never recognizes the URL

using System.Text;
using System.Threading;
using Newtonsoft.Json;
using System.Net;
using System.IO;
using WatiN.Core;


namespace testConsole
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            try
            {
                IE ie = new IE("https://www.google.com");
                ie.TextField(Find.ByName("q")).Value = "Youtube KeyEventHandler";
                ie.NativeDocument.Body.SetFocus();
                ie.Button(Find.ByName("btnG")).Click();

                Console.WriteLine(ie.Div(Find.ById("resultStats")).Text);

            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}

enter image description here

1

There are 1 best solutions below

0
Node defender On

According to your requirement you should use Winforms to open a browser and jump to the target website:

  1. Create a Windows Forms App (.Net Framework)

enter image description here

  1. Drag a button and double-click the button

enter image description here

enter image description here

  1. Refer to the following code:

     private void button1_Click(object sender, EventArgs e)
     {
    
         string explorpath = "C:/Program Files/Google/Chrome/Application/chrome.exe"; // Browser path, directly enter "explorer.exe" as the default browser
    
         string site = "https://test"; //destination site url
    
         System.Diagnostics.Process.Start(explorpath, site);
     }
    

After running, click the button to use a specific browser to visit a specific website