Home > front end >  How to open a new WINDOW in selenium
How to open a new WINDOW in selenium

Time:03-10

I want to open a completely new window in selenium. NOT a new tab, I need to completely close the chrome window and open a new one.

My code is kinda private and I don't want to share it (unless absolutely necessary) but I don't think it's required.

I am using chrome and using selenium in python.

Any help is appreciated

CodePudding user response:

In order to open a new window with selenium, not a new tab, as you mentioned, you can define a new driver object, something like

driver1 = webdriver.Chrome(executable_path='chromedriver.exe')

And use it additionally to the previously created driver instance.

CodePudding user response:

Once you completely close the existing chrome window the existing Web Browsing Session completely gets destroyed.

So to open a new chrome window you have to initialize a new ChromeDriver and a new Browsing Context i.e. combo using the following code block:

compatible code block

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

options = Options()
options.add_argument("start-maximized")
s = Service('C:\\BrowserDrivers\\chromedriver.exe')
driver = webdriver.Chrome(service=s, options=options)
driver.get('https://www.google.com/')

CodePudding user response:

A new window would be a new webdriver. However you opened the first window, just do it again.

But the two webdriver references share nothing between them, they are totally independent.

I assume your hangup is that you want to keep state from your previous webdriver in the new one, if that's the case, you will need to implement some sort of userData in the old one so that when you open the new one, you can transfer over your custom stuff before blowing away the old webdriver.

  • Related