Jialu Zhu
Jialu Zhu

Reputation: 69

Switching Frame in Selenium Webdriver, C#

I am getting this error using Selenium: An unhandled exception of type 'System.InvalidOperationException' occurred in WebDriver.dll

Additional information: '[JavaScript Error: "a is null" {file: "file:///C:/Users/jzhu/AppData/Local/Temp/anonymous1925480623.webdriver-profile/extensions/[email protected]/components/command_processor.js" line: 7509}]' when calling method: [nsICommandProcessor::execute]

Here is my code for this part:

ffbrowser.FindElement(By.XPath("//td[4]/a")).Click();

                            Thread.Sleep(1000);

                            ffbrowser.SwitchTo().Frame("content");

                            Thread.Sleep(1000);

                            ffbrowser.SwitchTo().Frame("NavBar");

                            Thread.Sleep(500);

                            ffbrowser.FindElement(By.XPath("//td[12]/a/div/img")).Click();

                            Thread.Sleep(800);

                            InputSimulator.SimulateKeyPress(VirtualKeyCode.RETURN);

                            Thread.Sleep(2000);

                            InputSimulator.SimulateTextEntry(@"Tostring.pdf");

                            Thread.Sleep(1500);

                            InputSimulator.SimulateKeyPress(VirtualKeyCode.RETURN);

                            Thread.Sleep(3500);

                            //ffbrowser.Navigate().Refresh();

                            //ffbrowser.SwitchTo().DefaultContent();


                            ffbrowser.Navigate().Back();
                            //InputSimulator.SimulateKeyPress(VirtualKeyCode.BROWSER_BACK);

                            Thread.Sleep(1000);

                            InputSimulator.SimulateKeyPress(VirtualKeyCode.RETURN);

                            Thread.Sleep(1000);

                            InputSimulator.SimulateKeyPress(VirtualKeyCode.RETURN);

                            Thread.Sleep(500);

                            //Thread.Sleep(500);

                            //ffbrowser.SwitchTo().DefaultContent();

                            //Thread.Sleep(1000);

                            //ffbrowser.SwitchTo().Frame("content");

                            //ffbrowser.SwitchTo().Frame("leftnav");

                            //Thread.Sleep(1000);

                            //ffbrowser.FindElement(By.LinkText("My Bills")).Click();

                            //Thread.Sleep(1000);

It is on a website behind a login, so I can't provide the actual example without giving sensitive login information. But I am using switchTo to get inside a frame, then I need to do Navigate.Back(), but it crashes with that error when I try to navigate.

Upvotes: 0

Views: 3807

Answers (1)

bbbco
bbbco

Reputation: 1530

I have very limited knowledge about C#, but I do know that there are errors thrown in other bindings (like Java and Ruby) if you try to call methods on the WebDriver object while inside an iframe object.

The solution is to use the webdriver.switchTo().defaultContent() method before attempting to webdriver.Navigate.Back().

Upvotes: 2

Related Questions