You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Selenium弹窗触发新窗口时各类等待失效,仅Thread.sleep可用求优化

问题:Selenium弹窗新窗口加载时动态等待失效,仅Thread.sleep可用

当弹窗弹出并打开新窗口时,代码会继续执行,不会等待新窗口完全加载。本该失败的测试意外通过,因为大量Web元素操作根本没执行。我试过implicitly wait、explicit wait、fluent wait都没用,只有Thread.sleep能正常工作,但这不是高效方案——动态等待才是正确选择,Thread.sleep会拖慢Selenium测试。我猜测试通过是因为剩余代码在while循环里。

有人建议在打开弹窗前获取并保存主窗口句柄:

String Parent=driver.getWindowHandle();

但这个方法对我没用。


相关代码

BaseClass(基于TestNG)

System.setProperty("webdriver.chrome.driver", "Drivers/chromedriver");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://www.usda.gov/nutrition-security");

测试类WindowHandles

package testNGpckg;

import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.*;


public class WindowHandles extends BaseClass {
    @Test
    public void handleWindow() throws InterruptedException {
        String parent = driver.getWindowHandle(); // 获取主窗口句柄
        Set <String> setOfWindows = driver.getWindowHandles(); // 获取所有窗口句柄
        Iterator <String> iterating = setOfWindows.iterator();// 迭代器遍历

        driver.findElement(By.xpath("/html/body/div/footer/div[2]/div[2]/div/div/div/div/a")).click();
        driver.switchTo().alert().accept();// 处理弹窗
        //Thread.sleep(4000); // 需替换为动态等待

        // 新窗口需在下方代码执行前打开
        while (iterating.hasNext() ) {
            String child = iterating.next();
            if (!parent.equalsIgnoreCase(child)) {
                driver.switchTo().window(child); // 切换到子窗口
                System.out.println(driver.getTitle()+ " (This is the Title of child window)");
                driver.findElement(By.xpath("/html/body/div/div[2]/div[1]/form/div/div[2]/div[2]/fieldset/div[3]/label")).click();
                WebElement email =  driver.findElement(By.xpath("//*[@id=\"inputProp0\"]"));
                email.sendKeys("shani@gmail.com");
                driver.findElement(By.xpath("//*[@id=\"update-profile-submit-btn\"]")).click();
                System.out.println("\n" + driver.findElement(By.xpath("//*[@id=\"optinSuccess\"]")).getText());
                // 切回主窗口
                System.out.println(" \n LETS TRY GOING BACK TO MAIN WINDOW AND GET TITLE AGAIN. \n "); 
                driver.switchTo().window(parent);
                System.out.println(driver.getTitle() +" (We are back to main window and this is the Title of main window)");
            }
        }
    }
}

我自学编程快3年了,没有学位,现在40多岁,恳请不要打击我——我终于找到了自己热爱的事情。

内容的提问来源于stack exchange,提问作者Salman Saleem

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 17:27:53