【问题标题】:Loading a URL in a frame using Selenium c#使用 Selenium c# 在框架中加载 URL
【发布时间】:2014-02-28 12:32:30
【问题描述】:

我正在尝试使用以下代码在框架中加载 URL,但该 URL 只是在主窗口中加载。

        WebDriver.SwitchTo().DefaultContent();
        WebDriver.SwitchTo().Frame("mainContent").Navigate().GoToUrl(_baseUrl); 

知道如何将 URL 直接加载到框架中吗?

【问题讨论】:

  • 一个框架 = 一个 iframe。这背后的意图是什么?你想解决什么问题?
  • 我正在为使用页面工厂页面对象创建的页面执行 ExecuteLoad。该应用程序有一个仅包含导航和 iframe 的主窗口。内容页面在“mainContent”iframe 中加载。我正在尝试在 iFrame 中加载页面而不需要导航。
  • 我认为您需要为此使用JavacriptExecutor

标签: c# selenium frames


【解决方案1】:

这是使用脚本执行器和脚本的一种方法。
使用驱动程序激活要加载的帧。

//Driver implements IJavaScriptExecutor, like 
// OpenQA.Selenium.IE.InternetExplorerDriver 
// that inherits OpenQA.Selenium.Remote.RemoteWebDriver 
// which implements implements IJavaScriptExecutor
InternetExplorerDriver driver = new InternetExplorerDriver(@".");
driver.SwitchTo().DefaultContent();

使用驱动程序执行加载激活帧中url的脚本

string _baseUrl = "www.google.com";
// the script that should be executed
string javascript = string.Format("window.location.href = \"{0}\";", _baseUrl);
try
{
    driver.ExecuteJavaScript<int>(javascript);
}
catch (NullReferenceException e)
{ //not sure why a null reference exception is thrown}

【讨论】:

  • 您应该在答案中的代码中添加一些解释。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-01
  • 2018-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多