¿Es posible hacer que DomContainer devuelva el Iframe cuando un elemento está en un IFrame?

I'm using Watin and I want to run some custom javascript. I can't use Browser.Eval because sometimes i will want to execute the code within an IFrame. I have tried using the DomContainer property to this but DomContainer returns the complete DOM not the DOM of the IFrame. Example below:

   Element.DomContainer.Eval(jScript)

Is there a way to do this using the Element object?

I want to avoid having two constructors. (See Below):

 public class TelerikWatinTextBox: ITextBox
{
    public TelerikWatinTextBox(Frame frame, string clientId)
    {
        Frame = frame;
        ClientId = clientId;
    }

    public TelerikWatinTextBox(Browser browser, string clientId)
    {
        Browser = browser;
        ClientId = clientId;
    }

    public string ClientId { get; set; }
    public Browser Browser { get; set; }
    public Frame Frame { get; set; }
    #region ITextBox Members

    public string Value
    {
        get
        {
            string value = "";
            string jScript = string.Format(@"$find(""{0}"").get_value();", ClientId);
            if (Browser != null)
            {
                value = Browser.Eval(jScript);
            }
            else if (Frame != null)
            {
                value = Frame.Eval(jScript);
            }

            return value;
        }
        set
        {
            string jScript = string.Format(@"$find(""{0}"").set_value(""{1}"");", ClientId, value);
            if (Browser != null)
            {
                Browser.Eval(jScript);
            }
            else if (Frame != null)
            {
                Frame.Eval(jScript);
            }
        }
    }

    #endregion
}


public interface ITextBox
{
    string Value { get; set; }
}

N.B. The clientId is a reference to a DIV.

preguntado el 09 de marzo de 12 a las 14:03

Could you explain better your needs? For example, why can't you distinguish the two cases calling Browser.Eval in the first case and Browser.Frames[x].Eval(jScript) in the second? -

I have telerik controls in my web pages and the best way to interact with them using watin is to execute some javascript. Its not as simple as inspecting an Input element. For example, i have an interface ITextBox. If its a telerik textbox then i run JS. If its a standard input element i just set the value. To run the JS i need to find the DOM container of ITextBox. Sometimes this is a frame, other times its the browser. -

What kind of information do you have about the element you are looking for? do you know just that it is an element of type ITextBox? if you can extract its ID you can identify its ancestors to eventually identify the containing frame (or the document if there is no containing frame) -

I know the id. If its a telerik control the id is a reference to a DIV. If its a normal input box it is the id of the input element. See my edit. -

1 Respuestas

Prueba esto (no probado):

Frame iFrame = browser.Frame(Find.ByClass("ClassName"));
iFrame.Eval("script")

respondido 09 mar '12, 15:03

I have already implemented something similar to you suggestion, but the issue i have is that sometimes the element will be in an Iframe and other times it won't. - Al Polden

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.