Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用FirefoxDriver在iframe出现时立即触发操作_C#_.net_Iframe_Selenium Webdriver - Fatal编程技术网

C# 使用FirefoxDriver在iframe出现时立即触发操作

C# 使用FirefoxDriver在iframe出现时立即触发操作,c#,.net,iframe,selenium-webdriver,C#,.net,Iframe,Selenium Webdriver,vb.net 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"), pro

vb.net

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;
}
所以我处理超时异常,处理NoTouchElementException以检查元素是否可用,但有时激发,有时不激发

有没有更好的方法:

1-不要等待文档。准备就绪

2-监视第二个iframe内的锚点,并将其发射


谢谢你的帮助

切换到第二个
iframe
后,应使用
explicit
wait等待元素。但是,您必须确保
Selenium
能够在第二个
iframe

 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
//定义selenium查找元素时要等待的时间。
WebDriverWait wait=新的WebDriverWait(驱动程序,TimeSpan.FromSeconds(10));
IWebElement oo=等待直到((d)=>
{
返回d.FindElement(按Id(“ifrm2”);
});
//iframe发现如此,请使用switchTo()
ff.SwitchTo().Frame(“ifrm2”)

好吧,诀窍是,当切换到第一个iframe,而第二个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")

@你是什么意思。net以及您使用的确切语言绑定?另外,如果我正确地看到它,唯一的问题就是在第二个
iframe
中查找
a
标记?正确吗?@Saifur不仅找到它,等待它,一旦它出现,然后启动它,我使用vb.net/c#随便什么..谢谢你的帮助,但是,如果一步一步地等待spet,等待第一个iframe,然后等待第二个iframe呢?同样的实现。可能会编写一个自定义函数来等待元素。您几乎可以对任何元素使用
WebDriverWait
。例如,
iframe1
iframe2
,问题编辑,如果iframe2出现,锚也会出现,所以我猜等待iframe2是目标..是的,然后使用
wait
for
iframe
然后
切换到()
进入
iframe
内部,最后执行
oo。单击()
+1-在另一个用例中,我一直在努力使用
thread.sleep()
wait.task.delay()
的组合,以确保元素在引用它们之前存在。当看到
WebDriverWait
implementation-doh时,便士下降了。看起来我有很多(愉快的)重构要做:-)
//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")
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