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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33460620/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 21:42:29  来源:igfitidea点击:

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

javaselenium

提问by Rahul Chintakunta

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
remotely located.

我尝试使用 JNA Api 来获取活动窗口及其标题,但我的网页
位于远程位置。

Perfect solution is greatly appreciated.

非常感谢完美的解决方案。

Thanks

谢谢

回答by AGill

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. getTitle()returns a Stringand you can use one of the string methods to compare the title, for example:

driver.getTitle()将为您提供页面的标题,您可以使用该标题来确定您所在的页面或您是否在您想要的页面上,然后根据需要使用逻辑切换窗口。getTitle()返回 aString并且您可以使用其中一种字符串方法来比较标题,例如:

String title = getDriver().getTitle();

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

回答by Chris Hawkes

String title = driver.getTitle()

This will give you the title of the page which you can refer to using Selenium to figure out which page the driver is currently on.

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

回答by Constantin Trepadus

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 包围,实现断言或其他技术来获得你需要的结果。

回答by mingxing

//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;
          }

      }
}

回答by Leon Barkan

String title = driver.switchTo().window(handle).getTitle();