[英]C# Selenium Webdriver Find Element Within Iframe
I am having trouble finding an iframe. 我在查找iframe时遇到问题。 I want to switch to this iframe then click on an element within it.
我想切换到该iframe,然后单击其中的一个元素。
I have tried finding the iframe using Id, Xpath, TagName, and CssSelector but my test times out while looking for the element each time. 我尝试使用Id,Xpath,TagName和CssSelector查找iframe,但是每次在查找元素时,我的测试都会超时。
This is the iframe as it appears in the page source: 这是显示在页面源代码中的iframe:
<div xmlns="http://www.w3.org/1999/xhtml" id="dashboardView" style="display: block;">
<iframe id="dashboardViewFrame" border="0" scrolling="no" frameborder="0"
style="visibility: visible; height: 607px; width: 1280px; background-color: transparent;"
src="HtmlViewer.ashx?Dd_ContentId=6a8a44ae-2bd5-4f3c-8583-e777279ad4f2"></iframe>
</div>
<iframe xmlns="http://www.w3.org/1999/xhtml" id="dashboardViewFrame" border="0" scrolling="no"
frameborder="0" style="visibility: visible; height: 607px; width: 1280px; background-color:
transparent;" src="HtmlViewer.ashx?Dd_ContentId=6a8a44ae-2bd5-4f3c-8583-e777279ad4f2"></iframe>
Here is my current code: 这是我当前的代码:
public static bool IsAt
{
get
{
try
{
var dashboardiFrame = Driver.Instance.FindElement(By.Id("dashboardViewFrame"));
//todo switch to iframe
//todo find element within iframe
return true;
}
catch
{
return false;
}
}
}
Can someone please suggest a way to find the iframe and switch to it? 有人可以建议一种找到iframe并切换到它的方法吗?
The main problem was that my test opened a new window, but my test was looking for elements on the old window. 主要问题是我的测试打开了一个新窗口,但是我的测试是在旧窗口中寻找元素。 I resolved that by switching to the new page using:
我通过使用以下命令切换到新页面来解决该问题:
Driver.Instance.SwitchTo().Window(Driver.Instance.WindowHandles.Last());
Then I could switch to the iframe also by also using SwitchTo() as shown below: 然后,我也可以使用SwitchTo()切换到iframe,如下所示:
public static bool IsAt
{
get
{
try
{
Driver.Instance.SwitchTo().Window(Driver.Instance.WindowHandles.Last());
var DBViFrame = Driver.Instance.FindElement(By.Id("dashboardViewFrame"));
Driver.Instance.SwitchTo().Frame(DBViFrame);
var dataEntryButton = Driver.Instance.FindElement(By.Id("HyperlinkDataEntry"));
dataEntryButton.Click();
return true;
}
catch(Exception ex)
{
return false;
}
}
}
some times you have to sleep around 5 second till page load completely then find frame. 有时,您必须睡5秒钟左右,直到页面完全加载,然后找到框架。
try this 尝试这个
thread.sleep(50000); thread.sleep(50000); IwebElement Frame = Driver.SwitchTo().Frame("id of the frame");
IwebElement框架= Driver.SwitchTo()。Frame(“框架ID”);
//then any element inside frame should get by this line //然后框架内的任何元素都应通过此行
Frame.FindElement(By.id("ID of element inside frame"); Frame.FindElement(By.id(“框架内元素的ID”);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.