java 目标窗口在硒中关闭

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39531832/
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-03 04:26:50  来源:igfitidea点击:

Target window closed in selenium

javaselenium-webdrivertestng

提问by Manish B

In the attached screenshot,i want to click New Browser Window. I want to close the browser window and click on New Message window. I closed the browser window. But i am getting the exception

在附加的屏幕截图中,我想单击“新建浏览器窗口”。我想关闭浏览器窗口并单击新消息窗口。我关闭了浏览器窗口。但我得到了例外

org.openqa.selenium.NoSuchWindowException: no such window: target window already closed

Screenshot

截屏

Below are the details Screenshot

下面是详细 截图

Below is the code

下面是代码

@Test
public void testing()
{
    driver.manage().window().maximize();
    driver.get("http://www.seleniumframework.com/Practiceform/");
    driver.findElement(By.id("button1")).click();
    Set<String> handle=driver.getWindowHandles();
    for(String handles:handle){
       try{
          String text=driver.switchTo().window(handles).getPageSource();
          if(text.contains("Agile Testing and ATDD Automation")){
              System.out.println("Text found");
              driver.close();
              break;
          }
       }catch(Exception e){}
    }
    driver.switchTo().defaultContent();
    driver.findElement(By.xpath("//button[contains(text(),'New Message Window')]")).click();
    driver.quit();

回答by kotoj

I guess you try to back to original window using driver.switchTo().defaultContent();? This is not proper way.

我猜您尝试使用driver.switchTo().defaultContent();? 这不是正确的方法。

You should:

你应该:

  1. store the original window at the beginning of test:
  1. 在测试开始时存储原始窗口:

String winHandleBefore = driver.getWindowHandle();

String winHandleBefore = driver.getWindowHandle();

  1. click button, switch windows, do whatever

  2. To back to original window use:

  1. 单击按钮,切换窗口,执行任何操作

  2. 要返回原始窗口,请使用:

driver.switchTo().window(winHandleBefore);

driver.switchTo().window(winHandleBefore);

Answer based on:https://stackoverflow.com/a/9597714/4855333

答案基于:https: //stackoverflow.com/a/9597714/4855333

回答by Sudharsan Selvaraj

In your code, Set<String> handle=driver.getWindowHandles();will contains list of handles from the browser. this will also contains the window handle of current tab in its first position.

在您的代码中, Set<String> handle=driver.getWindowHandles();将包含来自浏览器的句柄列表。这还将在其第一个位置包含当前选项卡的窗口句柄。

so when you loop gets executed for the first time,the focus will be on the current window only.so as your code executes it closes the current window.

因此,当您第一次执行循环时,焦点将仅在当前窗口上。因此,当您的代码执行时,它会关闭当前窗口。

You need to get the current window handle first and before switching to any window just check whether the window that you need to switch is not current window handle.

您需要先获取当前窗口句柄,在切换到任何窗口之前只需检查您需要切换的窗口是否不是当前窗口句柄。

Look at below example.

看下面的例子。

String mainwindow = driver.getWindowHandle();
Set<String> handle=driver.getWindowHandles();
for(String handles:handle)
{
   if(!mainwindow.equals(handles)){
      try
    {
         String text=driver.switchTo().window(handles).getPageSource();
         if(text.contains("Agile Testing and ATDD Automation"))
            {
               System.out.println("Text found");
               driver.close();
               break;
             }
   }catch(Exception e)
      {

      }
   }
}