selenium icon indicating copy to clipboard operation
selenium copied to clipboard

Selenium webdriver does not work on already opened browser.

Open subrato29 opened this issue 7 years ago • 2 comments

Selenium webdriver does not work on already opened browser.

subrato29 avatar Feb 27 '19 02:02 subrato29

The process flow to launch a browser is

  1. Selenium bindings requests for a new session from the WebDriver server.
  2. Webdriver server will launch a new browsers based on the above request. If everything is fine it will return a Session Id to selenium bindings. This will be stored in the instance of WebDriver. Which means one Webdriver instance in your code refers to one instance of a live browser.
  3. Now every call that you make will use that session Id to communicate with the WebDriver and in turn to the browser.

It is obvious that Webdriver bindings does not communicate with the Browser directly. WebDriver server is the mediating layer. To your issue, there are two ways that you can have an already opened browser

  1. You launched the browser without requesting it from Webdriver server
  2. You launched it using WebDriver server. Now you are trying to connect other WebDriver instance to it.

In the first case as Selenium bindings have no information of the launched browser, it is not possible for us to reuse that instance of browser. In the second case you would need the session ID of the browser that was launched. Then you would need to create a WebDriver instance in your code that can reuse that instance ID. As far as I know there is no public API method that can enable you to set the session ID of a WebDriver instance.

I am not sure what the Selenium contributors will decide on your issue but this is the high level working principle that will go into action when you would want to connect to the existing browser.

@subrato29 Out of curiosity, what is the driving force for you to connect to an existing browser?

virenv avatar Feb 27 '19 03:02 virenv

@virenv explained it very well. Selenium itself doesn't start a browser but asks drivers to do it (ChromeDriver, GeckoDriver, etc.) They do certain changes to the way the browser is started (e.g. might enable remote debugging protocols for communication between the driver and the browser), so in most cases, it's impossible to attach to a running browser.

Here is the relevant part of WebDriver specification: https://w3c.github.io/webdriver/#sessions.

Closing this issue as it works as expected.

p0deje avatar Feb 27 '19 07:02 p0deje