0

The website has the following elements:

html

It has 3 different frames, how do I navigate myself to the desired frame? In my following code, using trial and errors I found that frameIndex = 1 allows me to find those elements (welcome, config, instruments, etc).

But does this index number stay the same all the time? Is there a more reliable way for me to know which frame is which?

[TestClass]
public class Test2
{
    IWebDriver driver;
    string url = "http://10.116.33.6/";

    [TestInitialize]
    public void TestSetup()
    {
        var IEOption = new InternetExplorerOptions();
        var IEService = InternetExplorerDriverService.CreateDefaultService();
        IEOption.IntroduceInstabilityByIgnoringProtectedModeSettings = true;
        IEOption.IgnoreZoomLevel = true;
        IEService.HideCommandPromptWindow = true;

        driver = new InternetExplorerDriver(IEService, IEOption);
        driver.Navigate().GoToUrl(url);

    }

    [TestMethod]
    public void NavigateMenu()
    {
        driver.SwitchTo().Frame(1);

        var welc = driver.FindElement(By.Name("welcome"));
        var conf = driver.FindElement(By.Name("config"))     ;
        var inst = driver.FindElement(By.Name("instruments"));
        var stat = driver.FindElement(By.Name("status"))     ;
        var help = driver.FindElement(By.Name("help"))       ;

        conf.Click();
    }       
}
Liren Yeo
  • 3,215
  • 2
  • 20
  • 41

2 Answers2

2

You can actually select an iFrame using the below methods: -

  • frame(index)
  • frame(Name of Frame [or] Id of the frame)

  • frame(WebElement frameElement)

  • defaultContent()

So you can switch by passing the any above information about the frame. Yes you need to switch everytime according to require action

As we can see your frame have different name like :- top, navigation etc. Use name of the frame to switch between them

Example:-

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

.... Perform your action on frame

driver.SwitchTo().defaultContent();

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

.... Perform your action on frame

driver.SwitchTo().defaultContent();

Hope it will help you :)

Shubham Jain
  • 16,610
  • 15
  • 78
  • 125
  • That works! However, after I switch to `.Frame("navigation")` and do my stuffs, if I switch frame again (to a different frame), it will throw me `NoSuchFrameException`, any idea why? – Liren Yeo Feb 03 '16 at 07:24
  • Try this -> after navigation and its action ....then Use - > driver.SwitchTo().DefaultContent(); and then use .Frame("content").. Have a try – Shubham Jain Feb 03 '16 at 07:27
  • That actually works! Thank you! But what is actually `DefaultContent()`? – Liren Yeo Feb 03 '16 at 07:30
  • DefaultContent() puts back to your control to main page instead from the selected frame ... Please accept the answer if it help you .. it will really help me and also help for the people who are facing same issue in future .. Thanks :) – Shubham Jain Feb 03 '16 at 07:31
  • Thank you for your help! :) – Liren Yeo Feb 03 '16 at 07:57
0

I have done this in the past using:

m_internetExplorerDriver.SwitchTo().DefaultContent();
//put your name or id instead, e.g. "navigation" instead of "menuframe"
m_internetExplorerDriver.SwitchTo().Frame("menuframe"); 

Basically you use the Frame() method to switch between frames..

In this question you can read how to identify the frame:

How to identify and switch to the frame in selenium webdriver when frame does not have id

Community
  • 1
  • 1
Jannik
  • 2,310
  • 6
  • 32
  • 61