Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webdriver SwitchTo()

I am beginner in webdriver started learning page object model

Here is the code i use:

public static class test1
{
    public static ISearchContext Driver
    {
        get
        {
            return webDriver;
        }
    }
}

public static class test2
{
    public static test3 test3
    {
        get
        {
            var Test3 = new test3();
            PageFactory.InitElements(test1.Driver, Test3 );
            return Test3 ;
        }
    }
}

public class test3
{
    public void SwitchToFrame()
    {
        test1.Driver.SwitchTo().Frame(webDriver.FindElement(By.XPath("some xpath")));

    }
}

this keeps throwing an error. SwitchTo is never given as option , Can any one tell me why , Please tell me how to get SwitchTo as option. Thanks in advance

like image 996
Anand S Avatar asked Jul 14 '26 15:07

Anand S


2 Answers

The ISearchContext interface is not the interface that exposes SwitchTo, IWebDriver does.

What's the reason for using ISearchContext specifically? If you change it to be IWebDriver it will work.

This code, for example:

public static IWebDriver Driver
{
    get
    {
        return webDriver;
    }
}

IWebDriver implements ISearchContext, and gives much more functionality. I'd highly suggest you use that instead of just ISearchContext alone, unless you have a particular reason for doing so.

like image 50
Arran Avatar answered Jul 17 '26 15:07

Arran


I believe that when switching to a frame it needs to be done using the frame ID, name, or index. So, if you are trying to access an element within a frame you would have to switch to the frame first then perform webDriver.FindElement(By.XPath("some xpath")). Also, once in the frame webdriver remains in the frame until you switch back to the top level using webdriver.switchTo().defaultContent(); (Java).

I usually prefix all of my frame switches using the defaultContent:
webdriver.switchTo().defaultContent().switchTo().frame("HeaderFrame");
or
webdriver.switchTo().defaultContent().switchTo().frame("HeaderFrame").switchTo().frame("subframe");

if I need to go into a frame within a frame. This way I am always starting from the top of the page.

Hope this helps.

like image 32
Dannie Avatar answered Jul 17 '26 15:07

Dannie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!