如何在Selenium中选择iframe? [英] How to select Iframe in Selenium?
本文介绍了如何在Selenium中选择iframe?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要选择id ="iframe00237"的iframe及其内部数据:
I want select Iframe with id="iframe00237" and data inside it:
这是HTML:
<iframe src="http://example/iframe/v2.html?id=5225dddd-588a-49c2-961e-e3417cf5a728 scrolling="no" frameborder="0" marginwidth="0" marginheight="0" width="100%" height="100%" allowfullscreen="true" webkitallowfullscreen="true" mozallowfullscreen="true" id="iframe09328" class="sm_iframe"></iframe>
<body>
<div>
<iframe src="http://example.com/main?id=aab0619b-a938-4f00-b545-742fff5e1118&crtdt=1461214262& scrolling="no" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen="allowfullscreen" id="iframe05443" class="sm_iframe" style="left: 0px; top: 0px; width: 730px; height: 411px;"></iframe>
</div>
<div>
<iframe src="http://example.com/main?id=5225dddd-588a-49c2-961e-e3417cf5a728& scrolling="no" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen="allowfullscreen" id="iframe00237" class="sm_iframe" style="left: 0px; top: 0px; width: 811px; height: 456px;"></iframe>
</div>
</body>
Iframe src="url"
网址是随机的.
我在Stackoverflow中找到并尝试使用代码:
I'm finding in Stackoverflow and try with code:
//Select parent frame
IWebElement parentFrame = driver.FindElement(By.XPath("//iframe[@id='iframe09328']"));
driver.SwitchTo().Frame(detailFrame);
//Current we on detailFrame continues to go to childFrame.
IWebElement childFrame = driver.FindElement(By.XPath("//iframe[@id='iframe00237']"));
driver.SwitchTo().Frame(childFrame);
在调试模式下,parentFrame
和childFrame
始终为null
值.
In debug mode, parentFrame
and childFrame
always null
values.
如何解决我的问题?我也尝试FindElement by ID
.
How to resolve my problem? I also try FindElement by ID
.
推荐答案
如果ID是静态的,则可以使用By.Id().参见下面的代码:
In case the ID is static, you can use By.Id(). See code below:
IWait<IWebDriver> wait = new DefaultWait<IWebDriver>(driver);
wait.Timeout = TimeSpan.FromSeconds(10);
wait.PollingInterval = TimeSpan.FromMilliseconds(300);
By by = By.Id("iframe09328");
try
{
// Switch to the parent iframe
wait.Until(d => d.FindElements(by).Count > 0);
IWebElement parentFrame = driver.FindElements(by).First();
driver.SwitchTo().Frame(parentFrame);
// Switch to the inner iframe
by = By.Id("iframe00237");
wait.Until(d => d.FindElements(by).Count > 0);
IWebElement childFrame = driver.FindElements(by).First();
driver.SwitchTo().Frame(childFrame);
}
catch (Exception)
{
throw new NoSuchElementException("Unable to find element, locator: \"" + by.ToString() + "\".");
}
如果这行不通,请给我们一个例外:找不到元素,定位符:......"
If this doesn't work, please give us the exception "Unable to find element, locator: ......"
这篇关于如何在Selenium中选择iframe?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文