How to connect to an already open browser?

前端 未结 5 1276
终归单人心
终归单人心 2021-01-04 04:45

I would really appreciate a guide on how to connect to an already open browser using Selenium Webdriver via C#.

This issue eats around 30% of my script development t

5条回答
  •  一生所求
    2021-01-04 05:34

    you can use below code sample to achieve this task

    IWebDriver WebDriver = null;
    
    
    try
    {
      System.Uri uri = new System.Uri("http://localhost:7055/hub");
      WebDriver = new RemoteWebDriver(uri, DesiredCapabilities.Firefox());
      Console.WriteLine("Executed on remote driver");
    }
    catch (Exception)
    {
      WebDriver = new FirefoxDriver();
      Console.WriteLine("Executed on New FireFox driver");
    }
    

    if a firefox browser is opened using a Firefox web driver, you can use Remote WebDriver to use that browser instance. So First I try to initialize a Remote Web Driver, if no instance is running, then try block will fail and Catch block will open the Firefox driver. Now for example, your script fails on a specific location and the browser remained open. Now again run that initialization code, the try block will pass this time and remote web Driver will use the existing opened browser. (No new browser window will open).

    Put this initialization in your Test Startup method
    This block of code saves my ample amount of time. I have also written a blog post on it. You can read it here. http://www.binaryclips.com/2016/03/selenium-web-driver-in-c-how-to.html

提交回复
热议问题