user3198015
user3198015

Reputation: 121

Loading a URL in a frame using Selenium c#

I'm trying to load a URL in a frame using the following code, but the URL is just loading in the Main Window.

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

Any idea how to load a URL directly into a frame?

Upvotes: 1

Views: 948

Answers (1)

Stefan Cronert
Stefan Cronert

Reputation: 31

Here is a way to use the scriptexecutor and a script.
Activate the frame to load by using the driver.

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

Execute the script that loads the url in the activated frame by using the driver

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}

Upvotes: 2

Related Questions