c# – 无法从sitecore找到定位框架/模态对话框并在其中执行操作

下面是我卡住的页面的屏幕截图:

《c# – 无法从sitecore找到定位框架/模态对话框并在其中执行操作》

以下是HTML的屏幕截图:

《c# – 无法从sitecore找到定位框架/模态对话框并在其中执行操作》

HTML

<iframe id="scContentIframeId0" frameborder="0" scrolling="auto" class="ui-dialog-content ui-widget-content ui-dialog-normal" src="/sitecore/shell/Applications/Dialogs/External link.aspx?hdl=F66BB6924018478682797A717320B5DB&amp;ro&amp;la=en&amp;sc_content" style="width: auto; min-height: 0px; max-height: none; height: 285px;"></iframe>
<div class="ui-dialog ui-widget ui-widget-content ui-corner-all ui-front ui-draggable ui-resizable" tabindex="-1" role="dialog" aria-describedby="scContentIframeId0" aria-labelledby="ui-id-1" style="position: absolute; height: auto; width: 500px; top: 117.5px; left: 308px; display: block; opacity: 1;">
   <div class="ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix">
      <span id="ui-id-1" class="ui-dialog-title">&nbsp;</span>
      <div class="ui-dialog-titlebar-buttonpane" style="position: absolute; top: 50%; right: 0.3em; margin-top: -10px; height: 18px;"><button class="ui-button ui-widget ui-state-default ui-corner-all ui-button-icon-only ui-dialog-titlebar-close" role="button" aria-disabled="false" title="close" style="position: relative; float: right; top: auto; right: auto; margin: 0px;"><span class="ui-button-icon-primary ui-icon ui-icon-closethick"></span><span class="ui-button-text">close</span></button><a class="ui-dialog-titlebar-restore ui-corner-all ui-state-default" href="#" role="button" style="display: none;"><span class="ui-icon ui-icon-newwin">restore</span></a><a class="ui-dialog-titlebar-collapse ui-corner-all ui-state-default" href="#" role="button" style="display: none;"><span class="ui-icon ui-icon-triangle-1-s">collapse</span></a><a class="ui-dialog-titlebar-maximize ui-corner-all ui-state-default" href="#" role="button"><span class="ui-icon ui-icon-extlink">maximize</span></a><a class="ui-dialog-titlebar-minimize ui-corner-all ui-state-default" href="#" role="button" style="display: none;"><span class="ui-icon ui-icon-minus">minimize</span></a></div>
   </div>
   <iframe id="scContentIframeId0" frameborder="0" scrolling="auto" class="ui-dialog-content ui-widget-content ui-dialog-normal" src="/sitecore/shell/Applications/Dialogs/External link.aspx?hdl=F66BB6924018478682797A717320B5DB&amp;ro&amp;la=en&amp;sc_content" style="width: auto; min-height: 0px; max-height: none; height: 285px;"></iframe>
   <div class="ui-resizable-handle ui-resizable-n" style="z-index: 90;"></div>
   <div class="ui-resizable-handle ui-resizable-e" style="z-index: 90;"></div>
   <div class="ui-resizable-handle ui-resizable-s" style="z-index: 90;"></div>
   <div class="ui-resizable-handle ui-resizable-w" style="z-index: 90;"></div>
   <div class="ui-resizable-handle ui-resizable-se ui-icon ui-icon-gripsmall-diagonal-se" style="z-index: 90;"></div>
   <div class="ui-resizable-handle ui-resizable-sw" style="z-index: 90;"></div>
   <div class="ui-resizable-handle ui-resizable-ne" style="z-index: 90;"></div>
   <div class="ui-resizable-handle ui-resizable-nw" style="z-index: 90;"></div>
</div>

我试图通过定位元素找到这个框架并在其中执行操作.但我无法这样做.尝试使用Frame id – scContentIframeId0,Xpaths,类名但没有结果.

下面是我的Selenium c#代码

IWebElement IFrame = driver.FindElement(By.Id("scContentIframeId0"));
            driver.SwitchTo().Frame(IFrame);
IWebElement LinkDescription = driver.FindElement(By.XPath("//*[@id='Text']"));
            LinkDescription.SendKeys("External Link");
IWebElement URL = driver.FindElement(By.XPath("//*[@id='Url']"));
            URL.SendKeys("www.irctc.co.in");
            IWebElement OKButton = driver.FindElement(By.XPath("//*[@id='OK']"));

            OKButton.Click();

请帮忙.

异常堆栈跟踪结果:

Test method ComcastnowTest.Content_Event_RSVP_US688833.EventRSVPlink threw exception: 
OpenQA.Selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"scContentIframeId0"}
  (Session info: chrome=52.0.2743.116)
  (Driver info: chromedriver=2.23.409699 (49b0fa931cda1caad0ae15b7d1b68004acd05129),platform=Windows NT 6.1.7601 SP1 x86_64)
Result StackTrace:  
at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
   at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
   at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(String mechanism, String value)
   at OpenQA.Selenium.Remote.RemoteWebDriver.FindElementById(String id)
   at OpenQA.Selenium.By.<>c__DisplayClass2.<Id>b__0(ISearchContext context)
   at OpenQA.Selenium.By.FindElement(ISearchContext context)
   at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(By by)
   at ComcastnowTest.Content_Event_RSVP_US688833.EventRSVPlink()

最佳答案 我认为这是时间问题,您应该尝试使用WebDriverWait等待DOM上的iframe然后切换到它: –

IWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(3));

//Now wait for iframe to available and then switch 
 wait.Until(ExpectedConditions.FrameToBeAvailableAndSwitchToIt("scContentIframeId0"));

//Now find desire element inside this iframe 
IWebElement LinkDescription = wait.Until(ExpectedConditions.ElementExists(By.Id("Text")));
LinkDescription.SendKeys("External Link");

//Now after performing all stuff inside iframe switch back to default content 
driver.SwitchTo().DefaultContent(); 
点赞