1

Problem

Selenium, by default, opens a new browser window when it runs. However, I want Selenium to run in an existing browser session (Google Chrome specifically, if that helps). I have found questions which address this request, but they end up giving solutions in Java or Python. I would like a valid solution in Javascript.

Note

I use Node.js, and have installed selenium-webdriver using npm, so it may help when forming your answer.

fr0stbyte
  • 51
  • 7
  • Do you mean that new brower is opened, or something else? – Gaj Julije Feb 10 '21 at 20:27
  • A new window of the same browser. For example, I usually keep all my tabs in one window. When Selenium runs, a new browser window is opened, and that's where all the code is executed. I use Google Chrome and have one window open, and when Selenium runs, I have two Chrome windows open. – fr0stbyte Feb 10 '21 at 20:29
  • Hm. Selenium use webdriver, so because of that you need a new instance of it. I did not see any solution where you manually open a chrome and automated script is runing in that manually opened browser. Maybe I missunderstand you ... Also if you run part of code that say something like: var driver = new webdriver.Builder() multiple times you will get multiple webdriver windows. – Gaj Julije Feb 10 '21 at 20:37
  • What I want is the ability to run Selenium in a pre-opened Chrome window and NOT create a new window for itself. – fr0stbyte Feb 10 '21 at 20:46
  • 1
    I understand. You can not do it in your manually open browser... What do you need is to create ONLY 1 object of the webdriver and use it like Global variable (in java it would be a static global variable). – Gaj Julije Feb 10 '21 at 20:52
  • @GajJulije I asked this question because when Selenium runs and opens a new window, I access a website that requires me to be logged in (as expected). However, I am not automatically logged in, and the program that I run is very time-sensitive, so I cannot afford to spend time attempting to log in. Also, I have seen solutions to this, but in different languages. See here: https://stackoverflow.com/questions/8344776/can-selenium-interact-with-an-existing-browser-session – fr0stbyte Feb 10 '21 at 21:01
  • @ fr0stbyte Ok. It is officialy unsupported by selenium, but you can give it a try. – Gaj Julije Feb 10 '21 at 21:08
  • @GajJulije However, I do not know how to 'translate' the Java or Python in there into Javascript. – fr0stbyte Feb 10 '21 at 21:11

1 Answers1

1

Start chrome with debug port:

<path>\chrome.exe" --remote-debugging-port=1559

And in selenium use :

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:1559")
driver = webdriver.Chrome(options=chrome_options)
PDHide
  • 15,234
  • 2
  • 19
  • 35