切换到默认帧会引发NullReferenceException [duplicate]

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

我正在使用C#中的硒和Nunit进行一些Web测试。我目前在使用iframe时遇到一些问题。在我的方法之一中,我想返回默认框架,但使用driver.SwitchTo()。DefaultContent();。抛出NullReferenceException。

我也尝试过切换到框架0和切换到父框架,同样的问题。

这是实际方法:

    public void OpenInfoTab(int timeout = 10)
    {
        while (true)
        {
            if (timeout-- < 0) throw new TimeoutException("Infotab Does not open");
            Console.WriteLine(timeout);
            element.Click(InfoTab);
            System.Threading.Thread.Sleep(100);
            try
            {
                element.SwitchTo(InfoFrame);
                element.IsDisplayed(Dip1);

                break;
            }
            catch (AssertionException)
            {
                driver.SwitchTo().DefaultContent();
            }
        }
    }

iframe是一个窗口,当您单击信息选项卡时将打开,该窗口始终存在,但在单击按钮之前为空。element.IsDisplayed(Dip1);如果期望的元素在切换后不在iframe中,则会抛出异常。如果发生这种情况,我想退出框架,然后再试一次。

但是由于某种原因,我的外框可能为空,因此我无法再次退出。

c# selenium iframe nunit nullreferenceexception
1个回答
0
投票

确定,我发现了问题。我是一个大白痴,从未在对象的初始化中继承驱动程序。

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