12

This might be a repeated question but I could not find any solution. Recently I found a related post Connecting Selenium WebDriver to an existing browser session but people suggested me to ask a new question.

If any one have tried connecting selenium webdriver to existing browser session that was earlier spawned by selenium itself and had success in doing so, please let me know.

I could find couple of suggestions to try on firefox and selenium 2.X version. But those suggestions do not work for selenium 3.X and there are no solutions for chrome browser.

I have tried all suggestions for Selenium 25.3, firefox v 46 and it works. But for Chrome with chrome driver , I am not able to make it work.

Edited:

Here is the code I have tried:

Starting a firefox driver

System.setProperty("webdriver.gecko.driver", System.getProperty("user.dir")+"/StartFirefoxSession_lib/geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");

Copied RemoteWebDriver source code and changed capabilities from private to protected.

protected Capabilities capabilities; 

Created a new class RemoteDriverEx extending the copied RemoteWebDriver class Changed the NEW_SESSION command issued by the original driver to GET_CURRENT_URL

Response response = execute(DriverCommand.GET_CURRENT_URL, Collections.EMPTY_MAP);

Then craeted a JUnit test to verify

But I am struck with exception

org.openqa.selenium.WebDriverException: No command or response codec has been defined. Unable to proceed
Build info: version: 'unknown', revision: 'unknown', time: 'unknown'
System info: host: 'WPANDBW7HYD', ip: '192.168.56.1', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_74'
Driver info: driver.version: RemoteWebDriver
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:154)

Full code shared @ https://drive.google.com/open?id=0Bz2XxuQQc24KdHVqR3BPaXowUnM

Jignesh
  • 31
  • 10
Panda Biswajit
  • 121
  • 1
  • 4
  • as far as I know selenium does not support this – mosaad May 31 '17 at 11:15
  • https://stackoverflow.com/questions/42066902/running-a-test-case-on-a-webpage-that-is-already-open/42067778#42067778 – mosaad May 31 '17 at 11:18
  • @mosaad Thanks for sharing the link. Did you observe the footnote? `With IE, it's possible to iterate over the open windows in the OS and find the right IE process to attach to` Was wondering if there is a thread on it. – undetected Selenium May 31 '17 at 13:48
  • In the question there is no mention of IE. Maybe update the question so the right person finds it – mosaad May 31 '17 at 13:57
  • @mosaad Just to clarify, its not about IE, and IE is the least concern. I am trying on Firefox and Chrome using selenium 3.X. I have updated the question. – Panda Biswajit Jun 01 '17 at 09:52
  • @PandaBiswajit I guess it was that other guy commenting who was asking about it – mosaad Jun 01 '17 at 10:28
  • @mosaad sorry, I think I miss understood – Panda Biswajit Jun 01 '17 at 12:50
  • I found this has been a long standing request. People have tried this in many technologies. Here is a suggestion using python https://stackoverflow.com/questions/37963785/cannot-attach-to-an-existing-selenium-session-via-geckodriver/37968826#37968826 and tried to implement and check same in java. – Panda Biswajit Jun 01 '17 at 13:01
  • I have edited the original question to include the source code that I have tried. – Panda Biswajit Jun 01 '17 at 13:26
  • https://stackoverflow.com/questions/36476111/is-it-possible-to-get-session-id-of-active-driver-opened-with-selenium-and-appiu/53669860#53669860 – Mate Mrše Jan 09 '19 at 12:15
  • In selenium grid this is possible. – Ja8zyjits Apr 16 '19 at 06:11

2 Answers2

5

It is possible in selenium all you need is a debugger address of the session you want to connect to. If you are wondering what is debugger address its nothing but the localhost address on which your session is running, it looks like localhost:60003. Now it will be different for each and every case. Below is process with c# code.

  1. Get debugger Address of browser you want to connect later using debug mode as shown in snapshot below. debug driver after browser launch to fetch value

enter image description here

  1. Now keep that browser running and to reconnect the same browser use below code.

ChromeOptions option = new ChromeOptions();

option.DebuggerAddress="localhost:60422";// we need to add this chrome option to connect the required session

driver = new ChromeDriver(option);

driver.Navigate().GoToUrl("https://www.google.com/");

Hope this helps!! let me know in comments if any clarification is required.

Krunal Patel
  • 257
  • 3
  • 10
  • Can you please explain exactly how you find the debugger address? What is that a screenshot of? – holastello Aug 21 '20 at 16:01
  • 1
    @holastello I have taken this screenshot when I was in Debug mode. one way to do is open debug mode, put a breakpoint on the driver and you can get debugger address of launched session. Another way is to launch chrome on the port number of your wish using "--remote-debugging-port=60422" (you can use a port number of your wish) and use that port address to connect to lauched chrome. Hope it helps – Krunal Patel Aug 22 '20 at 22:27
3

I managed to find a solution for Firefox in a hack way that works local:

First, you need to start a separate instance of browser (manual start) using the following arguments:

firefox.exe --marionette -profile C:\FirefoxTEMP

Above we open an instance of Firefox with --marionette turned on and we choose a fixed profile folder that were created just for selenium tasks.

Now, we will attach our automation to the already open Firefox window, by adding an argument to chose the same profile we started before.

Note: You must chose the same profile folder for the Webdriver use the open instance.

FirefoxOptions options = new FirefoxOptions();
options.AddArguments("--profile C:\\FirefoxTEMP");


driver = new FirefoxDriver(options);

driver.Navigate().GoToUrl("https://google.com");
R4wd0G
  • 41
  • 1
  • 1
  • 6