简体   繁体   English

如何在selenium中开通一个新的WINDOW

[英]How to open a new WINDOW in selenium

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.我想在 selenium 中打开一个全新的 window。不是新标签,我需要完全关闭 chrome window 并打开一个新标签。

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 .我正在使用 chrome 并在 python 中使用selenium

Any help is appreciated任何帮助表示赞赏

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为了打开一个新的window和 selenium,而不是一个新的标签,正如你提到的,你可以定义一个新的driver object,比如

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

And use it additionally to the previously created driver instance.并将它附加到先前创建的driver实例。

Once you completely close the existing chrome window the existing Web Browsing Session completely gets destroyed .一旦你完全关闭现有的 chrome window 现有的Web 浏览 Session完全被破坏

So to open a new chrome window you have to initialize a new ChromeDriver and a new Browsing Context ie combo using the following code block:因此,要打开一个新的 chrome window,您必须使用以下代码块初始化一个新的ChromeDriver和一个新的浏览上下文,即组合:

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/')

A new window would be a new webdriver.一个新的 window 将是一个新的网络驱动程序。 However you opened the first window, just do it again.然而你打开了第一个window,再做一次。

But the two webdriver references share nothing between them, they are totally independent.但是这两个 webdriver 引用之间没有任何共享,它们是完全独立的。

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.我假设你的挂断是你想在新的网络驱动程序中保留 state,如果是这样的话,你需要在旧的网络驱动程序中实现某种用户数据,这样当你打开新的网络驱动程序时,你可以转移在吹走旧的 webdriver 之前,先检查你的自定义内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM