<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内的锚点出现并触发
感谢您的帮助!
答案 0 :(得分:2)
切换到秒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")
答案 1 :(得分:0)
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