• <i id='OxQhT'><tr id='OxQhT'><dt id='OxQhT'><q id='OxQhT'><span id='OxQhT'><b id='OxQhT'><form id='OxQhT'><ins id='OxQhT'></ins><ul id='OxQhT'></ul><sub id='OxQhT'></sub></form><legend id='OxQhT'></legend><bdo id='OxQhT'><pre id='OxQhT'><center id='OxQhT'></center></pre></bdo></b><th id='OxQhT'></th></span></q></dt></tr></i><div id='OxQhT'><tfoot id='OxQhT'></tfoot><dl id='OxQhT'><fieldset id='OxQhT'></fieldset></dl></div>
    <tfoot id='OxQhT'></tfoot>

  • <small id='OxQhT'></small><noframes id='OxQhT'>

        <bdo id='OxQhT'></bdo><ul id='OxQhT'></ul>
      <legend id='OxQhT'><style id='OxQhT'><dir id='OxQhT'><q id='OxQhT'></q></dir></style></legend>

        无法在 IE 上使用 Java 中的 Selenium WebDriver 获取新窗口句柄

        时间:2023-07-13

        <small id='Ry0dZ'></small><noframes id='Ry0dZ'>

          <bdo id='Ry0dZ'></bdo><ul id='Ry0dZ'></ul>
            <tbody id='Ry0dZ'></tbody>
              1. <tfoot id='Ry0dZ'></tfoot>

                <legend id='Ry0dZ'><style id='Ry0dZ'><dir id='Ry0dZ'><q id='Ry0dZ'></q></dir></style></legend>
                • <i id='Ry0dZ'><tr id='Ry0dZ'><dt id='Ry0dZ'><q id='Ry0dZ'><span id='Ry0dZ'><b id='Ry0dZ'><form id='Ry0dZ'><ins id='Ry0dZ'></ins><ul id='Ry0dZ'></ul><sub id='Ry0dZ'></sub></form><legend id='Ry0dZ'></legend><bdo id='Ry0dZ'><pre id='Ry0dZ'><center id='Ry0dZ'></center></pre></bdo></b><th id='Ry0dZ'></th></span></q></dt></tr></i><div id='Ry0dZ'><tfoot id='Ry0dZ'></tfoot><dl id='Ry0dZ'><fieldset id='Ry0dZ'></fieldset></dl></div>

                • 本文介绍了无法在 IE 上使用 Java 中的 Selenium WebDriver 获取新窗口句柄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  当我单击页面上的按钮(在 IE 浏览器中)时,会打开一个新的弹出页面.我尝试获取此弹出窗口的窗口句柄失败了.这是我的第一次尝试:

                  When I click a button on a page (in IE browser), a new popup page opens. My attempts to get the window handle for this popup have failed. Here is my first attempt:

                  String baseWin = driver.getWindowHandle();
                  System.out.println(baseWin);
                  Set<String>s = driver.getWindowHandles();
                  Iterator<String> ite = s.iterator();
                  while ( ite.hasNext() ) {
                      String popUpHandle = ite.next();
                      if(!baseWin.equals(popUpHandle)) {
                          driver.switchTo().window(popUpHandle);
                          System.out.println(driver.switchTo().window(popUpHandle).getTitle());
                  

                  此尝试仅打印基本窗口的句柄,如果第二个打印语句放置在 if() 语句之外,但在 while() 语句中和 if() 语句之后,它只会输出基本窗口的标题.所以这组句柄似乎只包含基本窗口句柄.

                  This attempt prints only the base window's handle, and if the second print statement is placed outside the if() statement, but within the while() statement and after the if() statement, it simply outputs the base window's title. So the set of handles only seems to contain the base window handle.

                  这是我的第二次尝试:

                  String baseWin = driver.getWindowHandle();
                  System.out.println(baseWin);
                  ArrayList<String> popUpWin = new ArrayList<String>(driver.getWindowHandles());
                  popUpWin.remove(baseWin);
                  driver.switchTo().window(popUpWin.get(0));
                  System.out.println(driver.switchTo().window(popUpWin.get(0)));
                  

                  此尝试返回错误,表示数组 popUpWin 为空,即 size == 0.因此,当我调用 driver.getWindowHandles() 时未检索弹出窗口的句柄,并且仅包含基本窗口的句柄.这是IE的问题吗?有解决方法吗?还是我忽略了代码中的某些内容?(请注意,我忽略了此处包含的代码中的暂停,因此我认为这不是问题所在.)

                  This attempt returns an error, which says that the array popUpWin is empty, i.e. size == 0. So, the popup window's handle isn't being retrieved when I call driver.getWindowHandles() and only contains the base window's handle. Is this an IE issue? Is there a workaround? Or am I overlooking something in my code? (Note that I've neglected pauses in the code that I've included here, so I don't believe that is the issue.)

                  推荐答案

                  你需要做两件事:

                  1. 更改IE浏览器的安全设置:

                  1. Change the security setting in IE browser:

                  打开IE浏览器,点击Internet选项"=>安全"=>勾选Internet"、本地Intranet"、受信任站点"和受限站点"的启用保护模式".

                  Open IE browser, click on "Internet Options" => "Security" => tick "Enable Protected Mode" for "Internet", "Local intranet", "Trusted sites" and "Restricted sites".

                  这使 IE 驱动程序能够控制新窗口句柄,这样当您调用driver.getWindowHandles();driver.getWindowHandles().size();您将获得所有句柄,包括原始窗口和新窗口.更准确地说,您只需将所有 4 个域的安全设置设置为相同,这意味着您可以取消选中所有 4 个域的启用保护模式",但显然不鼓励这样做.

                  This gives IE driver the capability to get control of the new window handle, so that when you call driver.getWindowHandles(); or driver.getWindowHandles().size(); you will get all the handles including the original window and the new windows. To be more accurate, you just need to set the security setting for all 4 domains to be the same which means you can uncheck "Enable Protected Mode" for all 4 domains but it is discouraged obviously.

                  调用driver.switchTo().window(windowName);后,需要添加((JavascriptExecutor) driver).executeScript("window.focus();"); 在 IE 驱动程序可以对窗口执行任何操作之前.

                  After you call driver.switchTo().window(windowName);, you need to add ((JavascriptExecutor) driver).executeScript("window.focus();"); before the IE driver can perform any actions on the window.

                  这是因为 IE 驱动程序需要它正在处理的窗口处于前台,这条线帮助驱动程序获得窗口的焦点,以便它可以在窗口上执行您想要的任何操作.

                  This is because IE driver needs the window that it is working on to be at the foreground, this line helps the driver get the focus of the window so that it can perform any actions on window that you want.

                  以下为完整示例:

                      String baseWin = driver.getWindowHandle();
                      //Some methods to open new window, e.g.
                      driver.findElementBy("home-button").click();
                  
                      //loop through all open windows to find out the new window
                      for(String winHandle : driver.getWindowHandles()){
                          if(!winHandle.equals(baseWin)){
                              driver.switchTo().window(winHandle);
                              //your actions with the new window, e.g.
                              String newURL = driver.getCurrentUrl();
                          }
                      }
                  
                      //switch back to the main window after your actions with the new window
                      driver.close();
                      driver.switchTo().window(baseWin);
                  
                      //let the driver focus on the base window again to continue your testing
                      ((JavascriptExecutor) driver).executeScript("window.focus();");
                  

                  这篇关于无法在 IE 上使用 Java 中的 Selenium WebDriver 获取新窗口句柄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何执行多个操作并通过 selenium-webdriver 在 URL http://www.spicejet.com 下一篇:Java 什么时候调用 finalize() 方法?

                  相关文章

                    <tfoot id='Xby0L'></tfoot>
                  1. <i id='Xby0L'><tr id='Xby0L'><dt id='Xby0L'><q id='Xby0L'><span id='Xby0L'><b id='Xby0L'><form id='Xby0L'><ins id='Xby0L'></ins><ul id='Xby0L'></ul><sub id='Xby0L'></sub></form><legend id='Xby0L'></legend><bdo id='Xby0L'><pre id='Xby0L'><center id='Xby0L'></center></pre></bdo></b><th id='Xby0L'></th></span></q></dt></tr></i><div id='Xby0L'><tfoot id='Xby0L'></tfoot><dl id='Xby0L'><fieldset id='Xby0L'></fieldset></dl></div>

                    <small id='Xby0L'></small><noframes id='Xby0L'>

                      <bdo id='Xby0L'></bdo><ul id='Xby0L'></ul>

                  2. <legend id='Xby0L'><style id='Xby0L'><dir id='Xby0L'><q id='Xby0L'></q></dir></style></legend>