Reputation: 145
I am quite new to Selenium and Visual Studio. I have created a test case to simply login to a web page and check for an element on the page having logged in. The login is fine and I get to the next page however I cannot find any element on the next page via any method such as XPath, Id or name. I get the following kinds of errors...
OpenQA.Selenium.NoSuchElementException : no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id='lblApplicationVersion']"}
I've also added some different waits such as...
new WebDriverWait(driver, TimeSpan.FromSeconds(30)).Until(ExpectedConditions.ElementExists((By.XPath("//*[@id='lblApplicationVersion']"))));
...but this just waits for the full duration even though the page has definitely loaded.
The elements html from chrome is as follows...
<html>
<head>...</head>
<frameset border="0" framespacing="0" rows="100,89%" frameborder="0">
<frame name="ob_frm_header" src="header.aspx" noresize="noresize" scrolling="no">
#document
<!DOCTYPE html>
<html>
<head>...</head>
<body>
<form method="post" action="./header.aspx" id="Form1">
<div class="aspNetHidden">...</div>
<script type="text/javascript">
...
</script>
<script type="text/javascript">
...
</script>
<script type="text/javascript">
...
</script>
<div id="header">
<div id="header_left">
<a id="OEMLogoLink" href="https://observatory.spikescavell.net/" target="_parent">...</a>
<div id="application_version" class="statuspanel_application_version">
<span id="lblApplicationVersion">v4.3.4</span>
</div>
</div>
<div id="MainMenu1" onresize="MainMenuControls.MainMenuStrip.prototype.sizeChanged($get('MainMenu1'));" style="overflow: hidden; width: 1644px;" menuname="Observatory_Main" submenuleftoffset="201" submenuframe="ob_frm_main" submenuid="SubMenu1">
</div>
<div id="statuspanel">...</div>
</div>
</form>
</body>
</html>
</frame>
<frameset id="frmset_ob_menu_main" cols="201,81%" border="0" framespacing="0" frameborder="0">
<noframes>...</noframes>
</frameset>
</html>
Upvotes: 1
Views: 1569
Reputation: 23805
It is good to hear you that, you have already found the solution, but you should share it as an answer that's why other users which have same problem could understand better that what is the actual problem.
So for this purpose I'm writing here the solution of your problem.
Actually frame
or iframe
is the separate HTML
document, so you have to switch this frame
or iframe
to set the current document before going to find element inside a frame
or iframe
as below :-
IWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
wait.Until(ExpectedConditions.FrameToBeAvailableAndSwitchToIt("ob_frm_header"));
IWebElement element= wait.Until(ExpectedConditions.ElementExists(By.Id("lblApplicationVersion")));
Or, if there is no need to implement WebDriverWait
, it can be simply achieve as below :-
driver.SwitchTo().Frame("ob_frm_header");
IWebElement element = driver.FindElement(By.id("lblApplicationVersion"));
Upvotes: 1