G. Dan
G. Dan

Reputation: 93

Unable to click button which is inside iframe and table in Selenium webDriver with c#

I am new to c# and I am working with Selenium chrome webdriver in c#. I am trying to click button which inside table. I am not able to identify and click button.The hierarchy of button is (Page > PopOver > PopOverFrame > Table1 > Table2 > Button to click )

Any help with this would be much appreciated thank you.

my code is :

          */
        // Closing old tab, keeping control in new tab and trying to perform click operation
        var currentWindow = BaseTest.Driver.CurrentWindowHandle;
        var availableWindows = new List<string>(BaseTest.Driver.WindowHandles);

        foreach (string mywindows in availableWindows)
            {

            if (mywindows != currentWindow)
                {
                Driver.SwitchTo().Window(mywindows).Close();
                }
            else
                {
                Driver.SwitchTo().Window(currentWindow);

                // performing click action on RUN REPORT button

                IWebElement popOver = Driver.FindElement(By.Id("popOver"));
                IWebElement popOverFrame = popOver.FindElement(By.Id("popOverFrame"))
                IWebElement table1 = popOverFrame.FindElement(By.XPath("//*[@id='form1']/table"));
                IWebElement Table2 = table1.FindElement(By.XPath("//*[@id='tblReport']"));
                Table2.FindElement(By.Id("contentPlaceholder_btnPrint")).Click();
                }
            }

Please refer to attached screenshot of what html page looks like.

<html xmlns="http://www.w3.org/1999/xhtml" ><title>

	<div id="divReportHeader">
        <span id="contentPlaceholder_lblReportDescHeader" class="reportHeader">Description</span>
        
        <br>
		<span id="contentPlaceholder_lblReportDescription">Offer</span>
		
	</div>
	<table>
		<tbody><tr>
			<td style="vertical-align: top;">
				<div style="margin: 2px; padding: 8px;">
					<table width="1000px" style="border-spacing: 0; padding: 0" id="tblReport">
                        <tbody><tr>
                            <td></td>
                        </tr>
						<tr>
							<td><div id="contentPlaceholder_generalInformation" class="reportHeader">Information</div></td>
						</tr>
						<tr>
							<td>
								<span id="contentPlaceholder_lblRptInfo">
                                    This report may require more information. Click "Run Report" to view the report inline.
								</span>
								<br>
							</td>
						</tr>
						<tr>
						<td></td>
						</tr>
						<tr>

						</tr>
                        <tr>
                            
                        </tr>
						<tr>
							<td>
								<span id="contentPlaceholder_lblfrom">abc </span>								
                                
								<select name="ctl00$contentPlaceholder$ddlfromYear" onchange="javascript:setTimeout('__doPostBack(\'ctl00$contentPlaceholder$ddlfromYear\',\'\')', 0)" id="contentPlaceholder_ddlfromYear">

</select>
															
								<span id="contentPlaceholder_lblFilter5" style="display: block; margin-top: 10px; margin-bottom: 4px;">Additional columns to be included:<br>Due to potential page size limitations, additional custom columns should only be selected when intending to view inline or export as Excel.</span>
								<input type="submit" name="ctl00$contentPlaceholder$btnSelectAll" value="Select All" id="contentPlaceholder_btnSelectAll" class="roundedButton">
								<input type="submit" name="ctl00$contentPlaceholder$btnDeselectAll" value="Deselect All" id="contentPlaceholder_btnDeselectAll" class="roundedButton" style="margin-bottom:5px;">
								<table id="chkList2" class="correctCheckboxes">
	<tbody><tr>hkList2_0" value="Select"><label for="chkList2_0">Help</label></td>
	</tr>
</tbody></table>
								
							
							</td>
						</tr>
						<tr>
							<td>
                                <br>
								<div id="contentPlaceholder_parameterNotes" class="reportHeader">
									 Notes
								</div>
                                <br>
                                <span id="contentPlaceholder_txtParameterNotes">None</span>
							</td>
						</tr>
						<tr>
							<td>
								
								
								
								
								
								

							</td>
						</tr>
						<tr>
							<td>
								
								<input type="submit" name="ctl00$contentPlaceholder$btnPrint" value="RUN REPORT" onclick="disable('contentPlaceholder_btnPrint');__doPostBack('ctl00$contentPlaceholder$btnPrint','');" id="contentPlaceholder_btnPrint" class="roundedButton" style="width:130px;">
								
                                
								
								
								
								
								
							</td>
						</tr>
					</tbody></table>
			</div></td>
		</tr>
	</tbody></table>
	
	

	
	<iframe id="ifrmDownload" style="display: none;"></iframe>
	<iframe id="ifrmStatus" src="statuscheck.aspx" style="display: none;"></iframe>

		</form>
	
</body></html>

Upvotes: 0

Views: 2227

Answers (2)

Bala
Bala

Reputation: 204

Try the below one:

driver.switchTo().frame(driver.findElement(By.xpath(iframeXpath)));

And once the operations are completed inside iFrame, switch back to default content.

driver.switchTo().defaultContent();

Upvotes: 1

Shahboz
Shahboz

Reputation: 546

Try this:

//remove this line IWebElement popOver = Driver.FindElement(By.Id("popOver"));
Driver.SwitchTo().DefaultContent();
IWebElement popOverFrame = Driver.FindElement(By.Id("popOverFrame"))
Driver.SwitchTo().Frame(popOverFrame);
//remove this line IWebElement table1 = popOverFrame.FindElement(By.XPath("//*[@id='form1']/table"));
IWebElement Table2 = Driver.FindElement(By.XPath("//*[@id='tblReport']"));
Table2.FindElement(By.Id("contentPlaceholder_btnPrint")).Click();

Upvotes: 1

Related Questions