简体   繁体   English

尝试使用 selenium / java 关闭选项卡并切换到另一个选项卡时出错

[英]Error trying to close a tab and switching to the other tab using selenium / java

I have a window with two tabs.我有一个带有两个标签的 window。 I am trying to close the tab with a specific title and switch the control to the other tab.我正在尝试关闭具有特定标题的选项卡并将控件切换到另一个选项卡。 Here is my code:这是我的代码:

public static void closeTheWindowWithTitle(String title) {

    ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());
    String mainWindow = driver.getWindowHandle();

    for(int i = 0; i < tabs.size(); i++) {

        log.debug("switched to " + driver.getTitle() + " Window");
        if(driver.getTitle().contains(title))
        {
            driver.switchTo().window(tabs.get(i));
            driver.close();
            log.debug("Closed the  " + driver.getTitle() + " Window");
        }
    }
    driver.switchTo().window(mainWindow);
}

When I run my code, I am getting the following exception:当我运行我的代码时,我收到以下异常:

org.openqa.selenium.NoSuchWindowException: no such window: target window already closed
from unknown error: web view not found

I am unable to figure out what is the problem.我无法弄清楚是什么问题。 Please help.请帮忙。

I'm guessing, that the WindowHandle of your Main-Window got changed, somewhere along the way.我猜,你的主窗口的 WindowHandle 已经改变了,沿途的某个地方。 You should be able to get your problem solved by doing something similar to the suggested solution here , ie getting all WindowHandles, and iterating over them and in the end switching to [0], which should be the only one left, after closing the second one.您应该能够通过执行与此处建议的解决方案类似的操作来解决您的问题,即获取所有 WindowHandles,并对其进行迭代,最后切换到 [0],这应该是唯一剩下的,在关闭第二个之后一。

I hope this will help you to fix your issue, i dont want to provide code fix and i want to explain you the details process step by step.我希望这将帮助您解决问题,我不想提供代码修复,我想逐步向您解释详细过程。

Open firefox/IE/Chrome browser and Navigate to https://www.bbc.co.uk打开 firefox/IE/Chrome 浏览器并导航到https://www.bbc.co.uk

WebDriver driver = new AnyDriveryourusing();
// set implicit time to 30 seconds
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
// navigate to the url
driver.get("https://www.bbc.co.uk");

Get the GU ID of the current (parent) window using getWindowHandle() method present in the webdriver and store the value in a String使用 webdriver 中的getWindowHandle()方法获取当前(父)window 的 GU ID,并将值存储在字符串中

// get the Session id of the Parent
String parentGUID = driver.getWindowHandle();

Click on the Open New Window button, application open new window with google page.单击打开新 Window 按钮,应用程序使用谷歌页面打开新 window。

// click the button to open new window
driver.findElement(By.id("two-window")).click();
Thread.sleep(5000);

Get the GU IDs of the two windows (parent + google), using getWindowHandles() method present in the webdriver .使用webdriver中的getWindowHandles()方法获取两个 windows(父级 + google)的 GU ID。 Store the GU IDs in a Set Collection, this Set will have GU IDs of both parent and Child Browsers将 GU ID 存储在 Set 集合中,此 Set 将具有父浏览器和子浏览器的 GU ID

// get the All the session id of the browsers
Set allGUID = driver.getWindowHandles();

iterate the Set of GUID values, and if the value is parent value skip it if not switch to the new window迭代 GUID 值集,如果该值是父值,则跳过它,如果不切换到新的 window

// iterate the values in the set
for(String guid : allGUID){
    // one enter into if block if the GUID is not equal to parent window's GUID
    if(! guid.equals(parentGUID)){
        //todo
    }
}

Switch to window using switchTo().window() method, pass the GU ID of the child browser to this method.使用switchTo().window()方法切换到 window,将子浏览器的 GU ID 传递给此方法。

// switch to the guid
driver.switchTo().window(guid);

Find the search bar in Google.com and search for "success"在Google.com中找到搜索栏,搜索“成功”

driver.findElement(By.name("q")).sendKeys("success");

Close the Google tab/Window and return to parent tab/browser window关闭 Google 选项卡/窗口并返回父选项卡/浏览器 window

// close the browser
driver.close();
// switch back to the parent window
driver.switchTo().window(parentGUID);

You were close but you weren't switching windows before checking the title.你很接近,但在检查标题之前你没有切换 windows 。 I've updated the code to something that should work.我已经将代码更新为应该可以工作的东西。

public static void closeTheWindowWithTitle(String title)
{
    Set<String> tabs = driver.getWindowHandles();
    String mainWindow = driver.getWindowHandle();

    for(int i = 0; i < tabs.size(); i++)
    {
        // you need to switch to the window before checking title
        driver.switchTo().window(tabs.get(i));
        log.debug("switched to " + driver.getTitle() + " Window");
        if(driver.getTitle().contains(title))
        {
            driver.close();
            log.debug("Closed the  " + driver.getTitle() + " Window");
            break; // this breaks out of the loop, which I'm assuming is what you want when a match is found
        }
    }
    driver.switchTo().window(mainWindow);
}

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

相关问题 使用 Selenium Web 驱动程序和 Java 切换到第一个选项卡 - Switching to First tab using Selenium Web Driver with Java 切换到新标签页的元素-硒 - Switching to elements of new tab - selenium 硒无法关闭新标签 - SELENIUM Unable to close new tab 尝试使用 selenium 4 打开新选项卡时,我看到“Selenium.InvalidArgumentException: invalid argument”错误 - I am seeing "Selenium.InvalidArgumentException: invalid argument" error when trying to open New Tab using selenium 4 使用 Selenium Java 清除网络选项卡 - Clear Network tab using Selenium Java (JAVA)选项卡声明错误(在构造函数中使用类选项卡) - (JAVA) Tab declaration error ( using a class tab in constructor ) 如何在Selenium 3中打开新选项卡中的链接并关闭硒鼓中的选项卡? - How to Open link in new tab and close tab in selenium 3? Selenium 2:在新标签页中打开链接并关闭标签页 - Selenium 2: Open link in new tab and close tabs 使用 Selenium 和 Java 时无法定位元素:{“method”:“xpath”,“selector”:“//li[@id=”tablist1-tab3“]”} 错误 - Unable to locate element: {“method”:“xpath”,“selector”:“//li[@id=”tablist1-tab3“]”} error using Selenium and Java 是否可以使用 java/spring boot 关闭浏览器的当前选项卡 - is it possible to close current tab of browser using java/spring boot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM