简体   繁体   English

如何在 Java 中不同 webdriver 打开的 chrome windows 之间切换?

[英]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 windows opened by two different WebDriver you can switch the focus using the following lines of code:要在两个浏览上下文之间切换,例如由两个不同的WebDriver打开的两个 windows,您可以使用以下代码行切换焦点

((JavascriptExecutor) driverA).executeScript("window.focus();");
((JavascriptExecutor) driverB).executeScript("window.focus();");

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

相关问题 如何使用带有Java的Selenium WebDriver在Chrome中的Windows之间切换? - How to switch between windows in Chrome using Selenium WebDriver with Java? 如何使用Java在Selenium Webdriver中的2个浏览器之间切换 - How to switch between 2 browsers in selenium webdriver with java 如何使用Java在Selenium WebDriver中切换帧 - How to switch between frames in Selenium WebDriver using Java 如何在 java 中使用 selenium 在两个或多个 chrome 浏览器 windows(不是标签)之间切换? - How to switch between two or mutlple chrome browser windows (Not tabs) using selenium in java? 如何在 Linux 中的不同 Java 版本之间切换? - How to switch between different java versions in Linux? 如何使用适用于 Chrome Windows 10 的 WebDriver 在 Eclipse (Java) 上安装 Selenium - How to Install Selenium on Eclipse (Java) with WebDriver for Chrome Windows 10 我可以在 java 中使用 selenium webdriver 通过页面标题在 windows 之间切换吗? - Can i switch between windows by its page title using selenium webdriver in java? 无法在Java中使用WebDriver切换到Chrome中的“当前活动标签页” - unable to switch to 'current active tab' in chrome using webdriver in java 如何在Java中检测打开的Windows - How to detect opened windows in java 无法使用Selenium WebDriver在两个浏览器窗口之间切换 - Unable to switch between two browser windows using Selenium WebDriver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM