繁体   English   中英

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

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

我有一个带有两个标签的 window。 我正在尝试关闭具有特定标题的选项卡并将控件切换到另一个选项卡。 这是我的代码:

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

当我运行我的代码时,我收到以下异常:

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

我无法弄清楚是什么问题。 请帮忙。

我猜,你的主窗口的 WindowHandle 已经改变了,沿途的某个地方。 您应该能够通过执行与此处建议的解决方案类似的操作来解决您的问题,即获取所有 WindowHandles,并对其进行迭代,最后切换到 [0],这应该是唯一剩下的,在关闭第二个之后一。

我希望这将帮助您解决问题,我不想提供代码修复,我想逐步向您解释详细过程。

打开 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");

使用 webdriver 中的getWindowHandle()方法获取当前(父)window 的 GU ID,并将值存储在字符串中

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

单击打开新 Window 按钮,应用程序使用谷歌页面打开新 window。

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

使用webdriver中的getWindowHandles()方法获取两个 windows(父级 + google)的 GU ID。 将 GU ID 存储在 Set 集合中,此 Set 将具有父浏览器和子浏览器的 GU ID

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

迭代 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
    }
}

使用switchTo().window()方法切换到 window,将子浏览器的 GU ID 传递给此方法。

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

在Google.com中找到搜索栏,搜索“成功”

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

关闭 Google 选项卡/窗口并返回父选项卡/浏览器 window

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

你很接近,但在检查标题之前你没有切换 windows 。 我已经将代码更新为应该可以工作的东西。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM