简体   繁体   English

如何使用 Selenium Java 在两个浏览器选项卡/窗口之间获取活动的 window 标题

[英]How to get an active window title between two browser tabs/windows using Selenium Java

I can switch between two tabs/windows but my requirement is to know or get active window between them.我可以在两个选项卡/窗口之间切换,但我的要求是知道或在它们之间激活 window。

In my project, on a click of a webElement of a page a random pop(tab/window) gets opened and I would like to know whether that(new) window has focus or my original page.在我的项目中,单击页面的 webElement 会打开一个随机弹出窗口(选项卡/窗口),我想知道那个(新)window 是否具有焦点或我的原始页面。

I tried to use JNA Api to get the active window and its title but my web page is我尝试使用 JNA Api 来获取活动的 window 及其标题,但我的 web 页面是
remotely located.位置偏远。

Perfect solution is greatly appreciated.非常感谢完美的解决方案。

Thanks谢谢

driver.getTitle() will give you the title of the page that you can use to determine which page you are on or if you are on the page where you want to be and then use the logic to switch window if required. driver.getTitle()将为您提供页面标题,您可以使用该标题来确定您所在的页面或您是否在您想要的页面上,然后根据需要使用逻辑切换窗口。 getTitle() returns a String and you can use one of the string methods to compare the title, for example: getTitle()返回一个String ,您可以使用其中一种字符串方法来比较标题,例如:

String title = getDriver().getTitle();

if(!title.equals("Expected Title")) {
        //may be you would like to switch window here
    } 
String title = driver.getTitle()

这将为您提供页面的标题,您可以使用 Selenium 参考该标题以找出驱动程序当前所在的页面。

If you want to assert the title, you could use the xpath selector:如果要断言标题,可以使用 xpath 选择器:

String pageTitle = driver.findElement(By.xpath("//title[text() = 'Title you looking for']"));

This is a dumb example with you can surround with try/catch, implement assertions or other technique to have the result you need.这是一个愚蠢的例子,你可以用 try/catch 包围,实现断言或其他技术来获得你需要的结果。

I wrote my own method to switch to a window if the window title is known, maybe some of this would be helpful.如果窗口标题已知,我编写了自己的方法来切换到窗口,也许其中一些会有所帮助。 I used Selenide (Java) methods for this, but if you've got Vanilla WebDriver, you can achieve the same thing我为此使用了 Selenide (Java) 方法,但是如果您有 Vanilla WebDriver,则可以实现相同的目的

/** Switches user to window of user's choice */
      public static void switchToWindow(String windowTitle) {
        WebDriver driver = getWebDriver();
        // Get list of all open tabs - note behaviour may be different between FireFox and Chrome.
        ArrayList<String> tabs = new ArrayList<>(driver.getWindowHandles());
        // iterate through open tabs. If the title of the page is contained in the tab, switch to it.
        for (String windowHandle : driver.getWindowHandles()) {
          String title = getWebDriver().getTitle();
          driver.switchTo().window(windowHandle);
          if (title.equalsIgnoreCase(windowTitle)) {
            break;
          }
        }
      }

This method might not be lightening fast, but it will iterate through current open windows and check the title matches the one you've specified.此方法可能不会很快变亮,但它会遍历当前打开的窗口并检查标题是否与您指定的标题匹配。

In JavaScript, for me this worked.在 JavaScript 中,这对我有用。 After clicking on the first link of bing search results in edge, my link opened in a new tab.在edge中点击bing搜索结果的第一个链接后,我的链接在新标签页中打开。 I explicitly mentioned to stay in the same tab.我明确提到要留在同一个选项卡中。

async function switchTab() {
      await driver.getAllWindowHandles().then(async function (handles) {
        await driver.switchTo().window(handles[1]);
      });
    }
//get initial window handles
Set<String> prevWindowHandles = driver.getWindowHandles();
while(true){
      //get current window handles
      Set<String> currWindowHandles = driver.getWindowHandles();
      //if one of the current window handles not equals to
      //any of the previous window handles,switch to this window
      //and prevWindowHandles = currWindowHandles
      for(String prevHandle : prevWindowHandles){
          int noEqualNum = 0;
          for(String currHandle : currWindowHandles){
              if(!currHandle.equals(prevHandle))
                  noEqualNum++
          }
          if(noEqualNum == currWindowHandles.size()){
             driver.switchTo().window(currWindow);
             prevWindowHandles = currWindowHandles;
                    break;
          }

      }
}

I can switch between two tabs/windows but my requirement is to know or get active window between them.我可以在两个选项卡/窗口之间进行切换,但我的要求是知道或获取它们之间的活动窗口。

In my project, on a click of a webElement of a page a random pop(tab/window) gets opened and I would like to know whether that(new) window has focus or my original page.在我的项目中,单击页面的webElement时,将打开一个随机弹出窗口(标签/窗口),我想知道该(新)窗口是否具有焦点或我的原始页面。

I tried to use JNA Api to get the active window and its title but my web page is我试图使用JNA Api来获取活动窗口及其标题,但是我的网页是
remotely located.位于偏远地区。

Perfect solution is greatly appreciated.非常感谢您提供完美的解决方案。

Thanks谢谢

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

相关问题 如何在 java 中使用 selenium 在两个或多个 chrome 浏览器 windows(不是标签)之间切换? - How to switch between two or mutlple chrome browser windows (Not tabs) using selenium in java? 如何使用Selenium java在浏览器中的两个窗口之间切换 - How to switch between two windows in browser using Selenium java 使用Selenium Java在浏览器中的两个窗口之间切换 - switch between two windows in browser using Selenium java Java - 获取活动窗口的标题 - Java - Get title of active window 使用带有 Java 的 Selenium WebDriver 在浏览器选项卡之间切换 - Switch between browser tabs using Selenium WebDriver with Java 用Java获取当前活动窗口的标题 - Get current active window's title in Java 无法使用Selenium WebDriver在两个浏览器窗口之间切换 - Unable to switch between two browser windows using Selenium WebDriver 如何在 selenium 测试中使用 java 代码检测两个 chrome 浏览器 windows 之间的音频连接? - How to detect audio connection between two chrome browser windows using jave code in selenium tests? 如何使用 Selenium Java 获取新打开的 IE 窗口的浏览器句柄 - How to get the browser handle for a new opened IE window using Selenium Java Selenium - 如何获取窗口中打开的选项卡数量? - Selenium - How to get the number of tabs opened in a window?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM