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
  • 2
  • 8
  • 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

2

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
  • 18,113
  • 2
  • 31
  • 46
  • How to run Selenium in new browser session? selenium opens in existing browser and tab and it neutralize previous actions – RF1991 Jan 16 '23 at 09:12
  • @RF1991 create a new tab and then start test , or create tab from test and switch to it before running the test – PDHide Jan 28 '23 at 10:39
  • what is the `chrome_options.add_experimental_option` or `options.add_argument` for that? – RF1991 Jan 28 '23 at 12:12
  • @RF1991 I am not getting your question , chrome_options.add_experimental_option , are to add chrome options that are not yet supported as methods of chrome_options means options which are not available like options.headless = True , https://www.selenium.dev/selenium/docs/api/py/webdriver_chromium/selenium.webdriver.chromium.options.html?highlight=chromeoptions#selenium.webdriver.chromium.options.ChromiumOptions options.add_argument are to add arguments to chrome like : --start-fullscreen https://peter.sh/experiments/chromium-command-line-switches/ – PDHide Jan 29 '23 at 23:55
  • I run two independent selenium code, while running the former, I run second code, and the latter runs in existing browser and it hence disrupt the running of first code, what should I do?using options.? – RF1991 Jan 31 '23 at 15:04