Unable to click with Selenium in nested frames

2019-08-30 10:37发布

问题:

I am trying to navigate on a page with nested frames. The page structure looks like this:

<frameset name="framesetContainer">
  <frame name="WebTopMenu">
  ...
  </frame>
  <frame name="WebContent">
    <frameset name="framesetTopContainer">
      <frameset name="framesetWSTopMenu">
        <frame name="frameTitle">
        ...
        </frame>
        <frame name="frameTopMenu">
        ...
        </frame>
      </frameset>
      <frameset name="framesetLeftMenuContentContainer">
        <frameset name="framesetLeftMenuContainer">
        ...
        </frameset>
        <frame name="frameContent">
        ...
        </frame>
      </frameset>
    </frameset>
  </frame>
</frameset>

The links to navigate reside in the frameTopMenu frame and the content is loaded into frameContent.

I am using the WebDriver API of Selenium (2.35.0). The following code runs fine without any exception, it finds the correct link but somehow the click() call does not have any effect and the content is not loaded into the inner frame.

driver.switchTo().frame("WebContent").switchTo().frame("frameTopMenu");
driver.findElement(By.id("link01")).click();

Do I miss something?

The frame structure cannot be changed...unfortunately.

回答1:

switch to any frame element , just use driver.switchTo().frame("framename");

Once we switched to one frame, if we need to switch to another frame, we have to switch to the parent frame.For that use

driver.switchTo().parentFrame();

If you use driver.switchTo().defaultContent();, it may not work. so go for driver.switchTo().parentFrame();, it works fine.



回答2:

Try below solution for nested frames.Hope it works

driver.switchTo().frame("WebContent").switchTo().
  frame("framesetTopContainer").switchTo().
  frame("framesetWSTopMenu").switchTo().
  frame("frameTitle");