【问题标题】:Unable to click button which is inside iframe and table in Selenium webDriver with c#无法使用 c# 在 Selenium webDriver 中单击 iframe 和表格内的按钮
【发布时间】:2018-04-04 16:03:37
【问题描述】:

我是 c# 新手,我正在 c# 中使用 Selenium chrome webdriver。我正在尝试单击表格内的按钮。我无法识别并单击按钮。按钮的层次结构是(Page > PopOver > PopOverFrame > Table1 > Table2 > Button to click )

对此的任何帮助将不胜感激。

我的代码是:

          */
        // 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>

【问题讨论】:

  • 如果您只是复制 html 代码而不是照片会很有帮助。
  • 您好 Hashim77,我添加了 html 代码。感谢您的帮助

标签: c# selenium selenium-webdriver c#-4.0 selenium-chromedriver


【解决方案1】:

试试这个:

//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();

【讨论】:

  • 答案的原因是,大多数带有iframe的页面都需要切换到才能与之交互。因此,您将首先切换到默认内容,然后再切换到层次结构中的第一个 iframe。
  • 谢谢你为我工作的哈希姆。非常感谢您的帮助
【解决方案2】:

试试下面的一个:

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

一旦在 iFrame 内完成操作,切换回默认内容。

driver.switchTo().defaultContent();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-17
    • 2021-11-09
    • 1970-01-01
    • 2021-01-08
    • 1970-01-01
    • 2016-03-02
    • 1970-01-01
    • 2021-12-20
    相关资源
    最近更新 更多