使用 FirefoxDriver 在 iframe 出现时立即触发操作
Fire an action as soon as an iframe appears using FirefoxDriver
<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-不等document.Ready
2- 监视直到第二个 iframe 内的锚点出现并触发它
感谢帮助!
切换到第二个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 时,第二个尚未加载,我应该切换回默认的主要内容..
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
<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-不等document.Ready
2- 监视直到第二个 iframe 内的锚点出现并触发它
感谢帮助!
切换到第二个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 时,第二个尚未加载,我应该切换回默认的主要内容..
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