[英]How to switch between chrome windows opened by different webdriver in Java?
When opening browsers using the same driver instance the switch works (followed https://www.browserstack.com/guide/handle-multiple-windows-in-selenium )but when I try to open two different windows with two different instance of web driver how to make it work? When opening browsers using the same driver instance the switch works (followed https://www.browserstack.com/guide/handle-multiple-windows-in-selenium )but when I try to open two different windows with two different instance of web驱动程序如何使它工作? I want to switch control to the first opened chrome window.
我想将控制切换到第一个打开的 chrome window。 Please help.
请帮忙。
You can use WinAppDriver if you do not have another option, it deals with window handles.如果没有其他选项,您可以使用 WinAppDriver,它处理 window 句柄。 It is used mainly for desktop applications.
它主要用于桌面应用程序。 It works for web applications, but is more time-consuming.
它适用于 web 应用程序,但更耗时。
I am not aware of such option in Selenium.我不知道 Selenium 中有这样的选项。 You can attach to already running Chrome driver, but as far as I know, there is no such functionality for the other drivers.
您可以附加到已经运行的 Chrome 驱动程序,但据我所知,其他驱动程序没有这样的功能。
References:参考:
How to switch focus between windows using WinAppDriver Java 如何使用 WinAppDriver Java 在 windows 之间切换焦点
https://harith-sankalpa.medium.com/connect-selenium-driver-to-an-existing-chrome-browser-instance-41435b67affd https://harith-sankalpa.medium.com/connect-selenium-driver-to-an-existing-chrome-browser-instance-41435b67affd
You can not do that.你不能这样做。 These are 2 different, totally separated web driver instances (processes) and you can not switch from one to another or pass windows handles between them during their run.
这是 2 个不同的、完全分离的 web 驱动程序实例(进程),您不能在它们运行期间从一个切换到另一个或在它们之间传递 windows 句柄。
To switch between two Browsing Contexts eg two google-chrome windows opened by two different WebDriver you can switch the focus using the following lines of code:要在两个浏览上下文之间切换,例如由两个不同的WebDriver打开的两个google-chrome windows,您可以使用以下代码行切换焦点:
((JavascriptExecutor) driverA).executeScript("window.focus();");
((JavascriptExecutor) driverB).executeScript("window.focus();");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.