【问题标题】:Fire an action as soon as an iframe appears using FirefoxDriver使用 FirefoxDriver 在 iframe 出现后立即触发操作
【发布时间】:2015-03-21 16:26:21
【问题描述】:
<iframe id="ifrm1">
    <head>
        <html>
            <body>
                <iframe id="ifrm2">
                    <head>
                        <html>
                            <body>
                                <a id="whatever" href="http://site1.com"></a>
                            </body>
                        </html>
                    </head>
                </iframe>
            </body>
        </html>
    </head>
</iframe>

c#

FirefoxProfile prof = new FirefoxProfile("D:\\Documents and Settings\\username\\Application Data\\Mozilla\\Firefox\\Profiles\\myporfile");
dynamic ff = new FirefoxDriver(new FirefoxBinary("D:\\Program Files\\Mozilla Firefox\\Firefox.exe"), prof, TimeSpan.FromMinutes(10));
ff.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(10));



try {
    ff.Navigate().GoToUrl("http://exemple.com");
    88:


    try {
        45:

        ff.SwitchTo().Frame("ifrm1");
        ff.SwitchTo().Frame("ifrm2");
    } catch (NoSuchFrameException exx) {
        goto 45;
    }

    try {
        66:
        IWebElement oo = ff.FindElement(By.TagName("a"));
        oo.Click();
        ff.Close();
    } catch (NoSuchElementException ex) {
        goto 66;
    }

} catch (WebDriverTimeoutException ex) {
    goto 88;
}

vb.net

 Dim prof As FirefoxProfile = New FirefoxProfile("D:\Documents and Settings\username\Application Data\Mozilla\Firefox\Profiles\myporfile")
        Dim ff = New FirefoxDriver(New FirefoxBinary("D:\Program Files\Mozilla Firefox\Firefox.exe"), prof, TimeSpan.FromMinutes(10))
        ff.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(10))



        Try
            ff.Navigate().GoToUrl("http://exemple.com")

88:         Try


45:             ff.SwitchTo().Frame("ifrm1")
                ff.SwitchTo().Frame("ifrm2")
   Catch exx As NoSuchFrameException
                GoTo 45
            End Try
            Try

66:             Dim oo As IWebElement = ff.FindElement(By.TagName("a"))
                oo.Click()
               ff.Close()
              Catch ex As NoSuchElementException
                GoTo 66
            End Try
        Catch ex As WebDriverTimeoutException

            GoTo 88
        End Try

所以我处理超时异常并处理 NoSuchElementException 以检查元素是否可用,但有时会触发,有时不会,

有没有更好的方法:

1- 不等待记录。准备好

2- 监视直到第二个 iframe 内的锚点出现并触发它

感谢您的帮助!

【问题讨论】:

  • @Saifur 你是什么意思? .net 以及
  • 您使用的是哪种语言绑定?另外,如果我正确地看到它,唯一的问题是在第二个iframe 内找到a 标签?对吗?
  • @Saifur 不仅找到它,等待它,一旦它出现然后触发它,我正在使用 vb.net/c# 任何东西..

标签: c# .net iframe selenium-webdriver


【解决方案1】:

切换到第二个iframe后,你应该用explicit等待元素等待。但是,您必须确保 Selenium 能够在第二个 iframe 内设置焦点

//Define the time you want to wait while selenium is looking for the element.
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement oo  = wait.Until<IWebElement>((d) =>
{
    return d.FindElement(By.Id("ifrm2"));
});

//iframe found so use switchTo()
ff.SwitchTo().Frame("ifrm2")

【讨论】:

  • 非常感谢您的帮助,但是,如果逐步等待,等待第一个 iframe,然后等待第二个 iframe 怎么办??
  • 相同的实现。可能会编写一个自定义函数来等待元素。您几乎可以将WebDriverWait 用于任何元素。如iframe1iframe2
  • 好吧,问题已编辑,如果出现 iframe2,锚点也会出现,所以我想等待 iframe2 是目标..
  • 是的,然后使用wait 替换iframe,然后使用switchTo() 进入iframe,最后使用oo.Click()
  • +1 - 在不同的用例上,我一直在努力结合使用thread.sleep()await.task.delay(),以确保元素在引用之前存在。看到WebDriverWait 实现-doh 时,便士掉了。看起来我有很多(快乐的)重构要做:-)
【解决方案2】:

好吧,诀窍是,当切换到第一个 iframe 并且第二个 iframe 尚未加载时,我应该切换回默认的主要内容..

ff.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(5))

//to start the job after 5 second , and skip waiting for all DOM document to be Ready !

   Try
            ff.Navigate().GoToUrl("http://exemple.com")

45         Try
           ff.SwitchTo().DefaultContent() // if ifrm1 is loaded and not iframe 2 , you should switch back to default !
           ff.SwitchTo().Frame("ifrm1")
           ff.SwitchTo().Frame("ifrm2")
           Dim oo As IWebElement = ff.FindElement(By.TagName("a"))
           oo.Click()
           ff.Close()
           Catch ex As Exception
                GoTo 45
            End Try
        Catch ex As WebDriverTimeoutException
//To Skip the wait of all document ...
            GoTo 45
End Try

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-27
    • 1970-01-01
    • 1970-01-01
    • 2021-09-27
    • 2020-03-29
    相关资源
    最近更新 更多