Can Selenium interact with an existing browser session?

后端 未结 11 890
野性不改
野性不改 2020-11-22 08:47

Does anybody know if Selenium (WebDriver preferably) is able to communicate with and act through a browser that is already running before launching a Selenium Client?

11条回答
  •  太阳男子
    2020-11-22 08:59

    It is possible. But you have to hack it a little, there is a code What you have to do is to run stand alone server and "patch" RemoteWebDriver

    public class CustomRemoteWebDriver : RemoteWebDriver
    {
        public static bool newSession;
        public static string capPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TestFiles", "tmp", "sessionCap");
        public static string sessiodIdPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TestFiles", "tmp", "sessionid");
    
        public CustomRemoteWebDriver(Uri remoteAddress) 
            : base(remoteAddress, new DesiredCapabilities())
        {
        }
    
        protected override Response Execute(DriverCommand driverCommandToExecute, Dictionary parameters)
        {
            if (driverCommandToExecute == DriverCommand.NewSession)
            {
                if (!newSession)
                {
                    var capText = File.ReadAllText(capPath);
                    var sidText = File.ReadAllText(sessiodIdPath);
    
                    var cap = JsonConvert.DeserializeObject>(capText);
                    return new Response
                    {
                        SessionId = sidText,
                        Value = cap
                    };
                }
                else
                {
                    var response = base.Execute(driverCommandToExecute, parameters);
                    var dictionary = (Dictionary) response.Value;
                    File.WriteAllText(capPath, JsonConvert.SerializeObject(dictionary));
                    File.WriteAllText(sessiodIdPath, response.SessionId);
                    return response;
                }
            }
            else
            {
                var response = base.Execute(driverCommandToExecute, parameters);
                return response;
            }
        }
    }
    

提交回复
热议问题