簡體   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