循环遍历 iframe 列表,检查 iframe 中的元素并对其执行操作

问题描述 投票:0回答:1

情况如下:在https://www.globalsqa.com/samplepagetest/上,我尝试通过页面下拉菜单测试者中心/演示测试站点/Alertbox访问页面“Alertbox”。在实际的 Alertbox 页面打开之前,会显示一个我想关闭的广告。广告位于 iframe 内,而 iframe 又嵌套在另一个 iframe 内。

现在这里变得有趣了......

页面加载时,有几个不同的 iframe(据我所知最多 7 个),并且每次加载页面时,带有广告的嵌套 iframe 都会随机出现在这 7 个 iframe 之一的下方。

我的想法是创建一个 iframe 列表和 for 循环,遍历所有 iframe 并检查嵌套的 iframe 是否位于其下方。当识别出正确的 iframe 时,切换到嵌套 iframe 并关闭广告。我还添加了 try/catch 方法来忽略拦截和 staleElement 异常。

由于我对循环的经验不够,我可能在代码中遗漏了一些东西。你能帮我一下吗?

我的代码:

@Test
    public void globalSQAHoverMenuTest() {
        driver.get("https://www.globalsqa.com/samplepagetest/");
        wdwait.until(ExpectedConditions.presenceOfElementLocated(By.id("menu-item-2822")));
        multiactions.moveToElement(driver.findElement(By.id("menu-item-2822"))).perform();
        wdwait.until(ExpectedConditions.presenceOfElementLocated(By.linkText("Demo Testing Site")));
        multiactions.moveToElement(driver.findElement(By.linkText("Demo Testing Site"))).perform();
        wdwait.until(ExpectedConditions.presenceOfElementLocated(By.linkText("AlertBox")));
        multiactions.moveToElement(driver.findElement(By.linkText("AlertBox"))).click().perform();

        List<WebElement> iframeList = driver.findElements(By.xpath("//iframe[@allowtransparency=\"true\"]"));
        try {
            for (WebElement webElement : iframeList) {
                wdwait.until(ExpectedConditions.visibilityOfAllElements(webElement));
                driver.switchTo().frame(webElement);
                if (!driver.findElements(By.id("ad_iframe")).isEmpty()) {
                    driver.switchTo().frame(driver.findElement(By.id("ad_iframe")));
                    wdwait.until(ExpectedConditions.presenceOfElementLocated(By.id("dismiss-button")));
                    driver.findElement(By.id("dismiss-button")).click();
                }
            }
        }

        catch (ElementClickInterceptedException e) {

        }

        catch (StaleElementReferenceException e) {

        }
    }
java for-loop iframe selenium-chromedriver iterator
1个回答
0
投票

根据您的评论,我将在该 wdwait 命令中添加一个 try catch ,以便在出现异常时不退出 for 函数。但这里最好的解决方案是检查该元素是否确实存在。

带有 try catch 的代码:

@Test
public void globalSQAHoverMenuTest() {
    driver.get("https://www.globalsqa.com/samplepagetest/");
    wdwait.until(ExpectedConditions.presenceOfElementLocated(By.id("menu-item-2822")));
    multiactions.moveToElement(driver.findElement(By.id("menu-item-2822"))).perform();
    wdwait.until(ExpectedConditions.presenceOfElementLocated(By.linkText("Demo Testing Site")));
    multiactions.moveToElement(driver.findElement(By.linkText("Demo Testing Site"))).perform();
    wdwait.until(ExpectedConditions.presenceOfElementLocated(By.linkText("AlertBox")));
    multiactions.moveToElement(driver.findElement(By.linkText("AlertBox"))).click().perform();

    List<WebElement> iframeList = driver.findElements(By.xpath("//iframe[@allowtransparency=\"true\"]"));
    try {
        for (WebElement webElement : iframeList) {
            try {
                wdwait.until(ExpectedConditions.visibilityOfAllElements(webElement));
            } catch (Exception e) {
                e.printStackTrace();
                continue;
            }
            driver.switchTo().frame(webElement);
            if (!driver.findElements(By.id("ad_iframe")).isEmpty()) {
                driver.switchTo().frame(driver.findElement(By.id("ad_iframe")));
                wdwait.until(ExpectedConditions.presenceOfElementLocated(By.id("dismiss-button")));
                driver.findElement(By.id("dismiss-button")).click();
            }
        }
    }

    catch (ElementClickInterceptedException e) {

    }

    catch (StaleElementReferenceException e) {

    }
}
© www.soinside.com 2019 - 2024. All rights reserved.