使用selenium,我需要动态迭代所有iframe及其子iframe [英] Using selenium, I need to iterate over all iframes and their child iframes on the fly

查看:122
本文介绍了使用selenium,我需要动态迭代所有iframe及其子iframe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要动态迭代所有iframe及其子iframe,因为我想对所有< img> 标记执行某些操作。我正在使用selenium。

I need to iterate over all iframes and their child iframes on the fly as I want to perform some action on all the <img> tags. I am using selenium.

public class logic {

static WebDriver driver;
static List<WebElement> TotaliinnerFrames=null;

logic() 
{
    driver=new FirefoxDriver();
    driver.get("aaaa.html");
}

public static void main(String[] args) 
{
    logic log=new logic();
    find_parent_iFrame();
}

private static void find_parent_iFrame() 
{
    List<WebElement> TotaliFrames =  driver.findElements(By.tagName("iframe"));
    System.out.println("TotaliFrames\t"+TotaliFrames.size());

    for(WebElement frame:TotaliFrames)
    {
        System.out.println("calling main frame==============================>\t");
        findLandingPage(frame);
    }

    if(TotaliFrames.size()>0)
    {
        driver.switchTo().defaultContent();
    }
    else
    {
        List<WebElement> canvas = driver.findElements(By.tagName("canvas"));
        List<WebElement> img = driver.findElements(By.tagName("img"));
        List<WebElement> anchor = driver.findElements(By.tagName("a")); 
        if(canvas.size()>0)
        {
            System.out.println("inside canvas");

            for(WebElement canvTemp : canvas)
            {
                callClick(canvTemp);
            }
        }

        if(img.size()>0)
        {
            System.out.println("inside img");
            for(WebElement imge : img)
            {
                callClick(imge);
            }
        }

        if (anchor.size()>0)
        {
            System.out.println("inside anchor");
            for(WebElement anchorc : anchor)
            {
                callClick(anchorc);
            }
        }

    }
}

private static void findLandingPage(WebElement frame) 
{
    driver.switchTo().frame(frame);

    List<WebElement> canvas = driver.findElements(By.tagName("canvas"));
    List<WebElement> img = driver.findElements(By.tagName("img"));
    List<WebElement> anchor = driver.findElements(By.tagName("a")); 
    System.out.println("canvas\t"+canvas.size());
    System.out.println("img\t"+img.size());
    System.out.println("anchor\t"+anchor.size());

    if(canvas.size()>0)
    {
        System.out.println("inside canvas");

        for(WebElement canvTemp : canvas)
        {
            callClick(canvTemp);
        }
    }

    if(img.size()>0)
    {
        System.out.println("inside img");
        for(WebElement imge : img)
        {
            callClick(imge);
        }
    }

    if(anchor.size()>0)
    {
        System.out.println("inside anchor");
        for(WebElement anchorc : anchor)
        {
            callClick(anchorc);
        }
    }

    TotaliinnerFrames = driver.findElements(By.tagName("iframe"));
    System.out.println("TotaliinnerFrames\t"+TotaliinnerFrames.size());
    int innerframesize=TotaliinnerFrames.size();
    if(innerframesize>0)
    {
        System.out.println("TotaliinnerFrames\t"+innerframesize);
        for(WebElement inframe:TotaliinnerFrames)
        {
            System.out.println("calling inner recursive\t");
            findLandingPage(inframe);
        }
        TotaliinnerFrames.clear();
    }
    else
    {
        driver.switchTo().defaultContent();
    }
}

private static void callClick(WebElement canvTemp) 
{
    try
    {
        canvTemp.click();
    }
    catch(Exception e)
    {
        System.out.println("Exception unable to click");
    }
}

}

推荐答案

您不能保留对 WebElement 跨页面加载(例如,通过点击启动),因为它们仅在自己的页面上有效。它们不仅仅是任何旧的Java对象。你会看到 StaleElementReferenceException

You can't hold references to WebElements across page loads (e.g. initiated by clicks), as they are only valid on their own page. They're not just any old Java object. You will get StaleElementReferenceException as you've seen.

逐页工作,而不是多页。

Work page by page, not across multiple pages.

编辑:这里的更好的解释

这篇关于使用selenium,我需要动态迭代所有iframe及其子iframe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆