Selenium WebDriver C# - 难以从iFrame切换回MainPage标题

时间:2013-06-07 16:27:32

标签: c# selenium webdriver automated-tests

善待,这是我使用硒和C#的第二个世界......

我正在测试IE中的网页,并且能够在模态弹出窗口/框架等之间切换。我在关闭当前页面(导航回到我的起点)之后遇到了麻烦,在页面上选择另一个主标题的元素?

所以这是开始,这是正常的(在你问之前,它是基于网络的酒店管理软件):

        // Navigate to and logon to page
        this.Login();

        // Selects the reservations main heading
        **this.ClickElementByID("rpt_TopMenu_ctl06_lbtn_MenuItem");** //This is a "MainMenu" item in question

        // Selects the reservations sub-heading
        this.ClickElementByID("x:1675617787.4:adr:0.2");

        // Switch to main page frame
        this.TargetFrame("MainFrame");

        // Selects 'create new reservation'
        this.ClickElementByID("wbtn_CreateIndividualReservation");

        //// Set text for title
        this.EnterTextByID("tc_tp_Main_rpt_Guests_ctl00_winp_Title", "Mr");

        //// Set text for firstname
        this.EnterTextByID("tc_tp_Main_rpt_Guests_ctl00_winp_Forename", "A");

        // Set text for lastname
        this.EnterTextByID("tc_tp_Main_rpt_Guests_ctl00_winp_Surname", "ABCDEFG");

它继续输入数据,直到我靠近。我的假设是它仍然在iFrame'MainFrame'上注册我。但是没有任何其他框架ID,我不知道如何切换回未展开的页面视图。我所在的框架位于我试图选择的元素标题下方(单独)。

标题确实有一个FormID,但不确定如何将焦点切换到这个?.....帮助......

任何批评都会很棒,因为我还在学习并且不想养成坏习惯(ps,我自己也很好,所以可能已经养成了一些不良习惯)。

-Colin

1 个答案:

答案 0 :(得分:3)

使用C#中的WebDriver,您可以获得如下所示的当前帧元素:

WebElement el = (WebElement) ((JavascriptExecutor) driver).executeScript("return window.frameElement");

在切换到MainFrame之前,您应该获取并存储此元素,以便您可以根据需要切换回来。