How to connect to already open browser in selenium using python?

Viewed 3440

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.

1 Answers

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)
Related