2

I have more than 4 chrome browser opened in the background now I want to connect one of the browsers that already open. how do I connect?

I have all chrome (that already open) URL and session id stored in DB. but I don't know how to connect to an already open browser after once I open different browsers?

Any type of help is appreciated and if any query regarding my question please ask me.

Mad's Man
  • 113
  • 1
  • 9
  • You can go with new tab in already open browser but at one time one browser instance only working otherwise you will get null point exception, and why are you opening 4 browsers in background? – Justin Lambert Sep 18 '20 at 12:17
  • https://stackoverflow.com/questions/63936575/how-to-do-scheduling-task-in-django-and-selenium-combine-project @JustinLambert please read this question for better understanding of my question. – Mad's Man Sep 18 '20 at 12:21

1 Answers1

1

Selenium has the option to handle the browser windows, you can use the method "window_handles()" which will return a list of windows or tabs identified by a UUID after you can switch to whatever you want, here is a piece of code that shows the usage of that method. Also if want to handle just one window you can use "window_handle()" which shows the current window UUID

#current window
    first_tab = browser.window_handles[0]
#create new tab
    browser.execute_script("window.open()")
#move to new tab
    new_tab = browser.window_handles[1]
    browser.switch_to.window(new_tab)
    browser.get('https://gmail.com')
#switch to first tab
    browser.switch_to.window(first_tab)
Alin Stelian
  • 861
  • 1
  • 6
  • 16
  • thank you for answer. but can any unique id per chrome so I know which browser exactly I want to open. because in my project every chrome is for one user. – Mad's Man Sep 18 '20 at 12:34
  • Also for more understanding of my question, you can watch this https://stackoverflow.com/questions/63936575/how-to-do-scheduling-task-in-django-and-selenium-combine-project – Mad's Man Sep 18 '20 at 12:35
  • please rephrase this "but can any unique id per chrome so I know which browser exactly I want to open. because in my project every chrome is for one user" - I don't understand it – Alin Stelian Sep 18 '20 at 12:38
  • one chrome associated with one user. chrome always opens in the background whenever the user logs in again I want to reconnect that user with chrome that already associated with them. for every user I have a different profile directory but whenever I try to connect it's say directory already in use. because I never close my chrome. that's I asking there is any unique id per chrome. – Mad's Man Sep 18 '20 at 12:53
  • that's a selenium limitation - you cannot access the same browser profile twice – Alin Stelian Sep 18 '20 at 13:00