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

时间:2016-09-09 13:09:36

标签: c# selenium-webdriver modal-dialog sitecore sitecore7

以下是我遇到的页面的屏幕截图:

enter image description here

以下是HTML的屏幕截图:

enter image description here

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

3 个答案:

答案 0 :(得分:1)

如果您的框架代码中有任何错误,则浏览器将不会执行该代码,也不会显示在您的网站中。因此,您应首先确认框架是否正确。

你可以试试这个:

<div id="anyNameToFind">
  <span>here is the frame in website</span>
  <frame></frame>
</div>

但是,您应首先查看您的工作天气。

答案 1 :(得分:1)

我认为这是时间问题,您应该尝试使用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(); 

答案 2 :(得分:0)

我们遇到了这个问题,最终通过切换到jqueryModalDialogsFrame然后scContentIframeId0找到了这个问题。

driver.SwitchTo().Frame("jqueryModalDialogsFrame");

然后

driver.SwitchTo().Frame("scContentIframeId0");