Moving back to parent frame in Selenium

前端 未结 2 936
无人共我
无人共我 2021-01-16 11:39

For moving back to parent frame in Selenium, driver.switchTo().parentFrame(); is used. In my example website, I achieved same functionality using driver.s

2条回答
  •  天命终不由人
    2021-01-16 12:05

    If you only have two frames on a page, there is no functional difference. However, the app I'm working on has as many as 5 nested frames on a page.

    For example, I need to work with two elements that are at the third frame, step inside another frame, then step back to work with another element in frame 3. I have two choices to accomplish this.

    1: I can step all the way out and then back in

    //click element 1
    driver.SwitchTo().DefaultContent();
    driver.SwitchTo().Frame(driver.FindElement(By.Id("Frame1")));
    driver.SwitchTo().Frame(driver.FindElement(By.Id("Frame2")));
    driver.FindElement(By.Id("element1")).Click();
    //click element 2
    driver.SwitchTo().DefaultContent();
    driver.SwitchTo().Frame(driver.FindElement(By.Id("Frame1")));
    driver.SwitchTo().Frame(driver.FindElement(By.Id("Frame3")));
    driver.FindElement(By.Id("element2")).Click();
    

    The other option is to switch to the parent frame.

    //click element 1
    driver.SwitchTo().DefaultContent();
    driver.SwitchTo().Frame(driver.FindElement(By.Id("Frame1")));
    driver.SwitchTo().Frame(driver.FindElement(By.Id("Frame2")));
    driver.FindElement(By.Id("element1")).Click();
    //click element 2
    driver.SwitchTo().ParentFrame();
    driver.SwitchTo().Frame(driver.FindElement(By.Id("Frame3")));
    driver.FindElement(By.Id("element2")).Click();
    

    Basically, it saves the effort of writing code to switch all the way back out then back into the frame if you only need to move back one level.

提交回复
热议问题