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

本文关键字:MainPage 标题 iFrame WebDriver Selenium | 更新日期: 2023-09-27 18:17:37

请原谅,这是我使用selenium和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,我是自学的,所以可能已经有一些坏习惯了)。

科林

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

使用c#中的WebDriver,您可以像这样获取当前框架元素:

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

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